admin管理员组

文章数量:1794759

搭建Spring4+Spring MVC web工程的最佳实践

搭建Spring4+Spring MVC web工程的最佳实践



Spring是个非常非常非常优秀的java框架,主要是用它的IOC容器帮我们依赖注入和管理一些程序中的Bean组件,实现低耦合关联,最终提高系统可扩展性和可维护性,用它来辅助我们构建web工程将会感觉非常非常非常地愉悦。

Spring旗下的Spring MVC又是后来居上,设计得非常非常非常的优雅,可以用来替代Struts来做界面视图的控制(Controller)等。

现在我们就来搭建一个利用Spring和Spring MVC结合的web工程最佳实践的例子。以Spring Framework 4.2.0为例,IDE为Myeclipse。

 

首先,New一个Dynamic Web Project

 

加入spring-context及其依赖的jar包

加入Spring MVC相关jar包

完整的jar包如下

 

现在开始准备配置Spring和Spring MVC的IOC容器,理论上说,可以只需要Spring MVC的IOC容器即可,所有的bean都放到里面让Spring MVC容器来管理,但是这样做并不优雅,我们可以让Spring MVC容器只管理和它本身相关的东西,像数据源、事务管理以及自己程序中需要用到的Bean等可以用Spring的IOC容器来管理。

 

在web.xml中配置以启动Spring的IOC容器:

<!-- 启动 Spring 的IOC容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

 

这段配置的意思是给ServletContext传入一个名为“contextConfigLocation”的配置信,然后添加一个Spring为我们提供好的用来启动Spring容器的监听器,web应用启动的时候这个监听器就会从ServletContext中取名”contextConfigLocation”的配置信,即Spring配置文件的所在路径,如果有就会从指定路径读取配置文件启动Spring容器,如果没有就从默认路径读取,这里我们指定为WEB-INF下的beans.xml文件,下面是一个最基本的beans.xml配置文件的例子:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:context="www.springframework/schema/context" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="com.cpwl"> </context:component-scan> </beans>

 

以上配置指定了需要扫描组件的包,base-package表示需要扫描的包,Spring会扫描它及其所有子包中的组件(加了某些注解的类,如:@Component、@Controller、@Service、@Repository等),然后将其创建实例并放入IOC容器。

 

然后我们再来配置一下启动Spring MVC容器的必要配置,回到web.xml,将下面的配置粘贴进去:

<!-- 启动 Spring MVC 的IOC容器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

 

因为Spring MVC主要是用来作为前端控制器,所以它底层自然是Servlet实现的咯,上面配置的意思是配置一个Spring为我们提供好的用来启动Spring MVC的Servlet,读取指定路径的Spring MVC配置文件,并指定它拦截所有请求(Spring MVC会将请求交给指定请求路径的Controller去处理)。这里我们指定的路径为WEB-INF目录下的spring-mvc.xml文件作为Spring MVC的配置文件。

 

来到WEB-INF目录,新建一个spring-mvc.xml文件,和beans.xml文件一样的格式。

 

我们主要利用Spring MVC来写Controller,每个Controller可以映射任意多个路径,利用注解来标注Controller非常方便和优雅,我们需要用到@Controller注解来指定Controller对象,用@RequestMapping来指定某方法映射某路径,这时只需要在spring-mvc.xml中加入<mvc:annotation-driven></mvc:annotation-driven>即可。

 

但是拦截所有请求,一些静态资源外界就不好访问,这时我们希望让服务器自身默认的Servlet去帮我们处理静态资源的响应,只需要再spring-mvc.xml文件里面配个<mvc:default-servlet-handler/>就行,用到了mvc命名空间,自然也就需要导入mvc命名空间了。

 

别急,还需要指定Spring MVC扫描组件的包,在spring-mvc.xml中加入类似这种的配置:<context:component-scanbase-package="com.cpwl"use-default-filters="false"></context:component-scan>

注意,use-default-filters置为false。

 

这时完整的spring-mvc.xml是这个样子的:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xsi:schemaLocation="www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-4.2.xsd www.springframework/schema/beans www.springframework/schema/beans/spring-beans.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.2.xsd"> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="com.cpwl" use-default-filters="false"> </context:component-scan> </beans>

 

现在,我们可以来写一个Controller了:

 

package com.cpwl.springtest.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class TestController { public TestController() { System.out.println("TestController constructed......"); } @RequestMapping(value="/test",method=RequestMethod.GET) public ModelAndView testMVC(){ ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/test.jsp"); modelAndView.addObject("info", "陈鹏万里"); return modelAndView; } }

 

该Controller的testMVC方法映射了”/test”的路径,访问它的时候它将设置一个信,然后转发给/WEB-INF/views/test.jsp来显示输出页面给客户端。

在WEB-INF目录下新建一个views目录,进入该目录再新建一个test.jsp,如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Spring MVC Test</title> </head> <body> Hello ${info} !!!!! </body> </html>

 

现在web工程的项目结构如下:

 

OK,现在我们将其部署到服务器,启动服务器后用浏览器访问testMVC()所映射的路径:

 

哎哟,不错哟。

 

但是!我们看下控制台输出的信:

TestConstructor被创建了两次,Why???

这是因为我们用了两个容器,一个Spring一个SpringMVC,给它们所指定扫描的包都相同,所以所有组件都会被创建两次。

怎么解决?两种方案。

 

方案一:把Spring MVC容器需要扫描的组件单独放到一个包下,比如:com.cpwl.springtest.controller,然后在component-scan的base-package属性指定为改包的包名,Spring容器也类似这样做。但是这样并不是很好,实际开发中有时很难做到这样。

 

方案二:component-scan中指定filter,举个栗子,

 

spring-mvc.xml中的component-scan这样写:

<context:component-scan base-package="com.cpwl" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>

 

beans.xml中的component-scan这样写:

<context:component-scan base-package="com.cpwl"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan>

 

意思是让Spring MVC的容器只扫描和Controller相关的注解,Spring的容器就只不扫描和Controller相关的注解,这样它们就相安无事,可以一起愉快地为我们服务了。

 

其实还可以改进一些,在spring-mvc.xml中加入如下配置:

 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>

 

配置一个内部资源视图解析器,假如你在Controller里返回一个视图,它的路径就可以简写了,省略前缀和后缀,之前我们写的是:/WEB-INF/views/test.jsp,现在可以简写成:test

我们只需要返回一个简写的内部资源路径的字符串就行,这样,我们也没必要创建ModelAndView了,直接让方法的参数给我们提供一个Map,我们向里面写点数据然后交给视图去显示就行了,testMVC方法改写如下:

 

@RequestMapping(value="/test",method=RequestMethod.GET) public String testMVC(Map<String,Object> map){ map.put("info", "陈鹏万里"); return "test"; }

 

用浏览器访问结果和之前一模一样,prettygood!

源码可以在这里下载:download.csdn/detail/qq245671051/8953273

 

 

初来乍到,经验尚浅,若有不对之处还望各位不吝赐教,欢迎指正  :)

本文标签: 工程springwebMVC