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

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

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

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

 

首先,New一个Dynamic Web Project

 

加入spring-context及其依赖的jar

加入Spring MVC相关jar

完整的jar包如下

 

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

 

web.xml中配置以启动SpringIOC容器:

 

[html] view plain copy
 
  1. <!-- 启动 Spring 的IOC容器 -->  
  2.   
  3. <context-param>  
  4.   
  5.     <param-name>contextConfigLocation</param-name>  
  6.   
  7.     <param-value>/WEB-INF/beans.xml</param-value>  
  8.   
  9. </context-param>  
  10.   
  11. <listener>  
  12.   
  13.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  14.   
  15. </listener>  

 

 

 

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

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:context="http://www.springframework.org/schema/context"  
  8.   
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.   
  11.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">  
  12.   
  13.    
  14.   
  15.     <context:component-scan base-package="com.cpwl">  
  16.   
  17.    
  18.   
  19. </context:component-scan>  
  20.   
  21.    
  22.   
  23. </beans>  

 

 

 

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

 

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

 

[html] view plain copy
 
  1. <!-- 启动 Spring MVC 的IOC容器 -->  
  2.   
  3. <servlet>  
  4.   
  5.   <servlet-name>springDispatcherServlet</servlet-name>  
  6.   
  7.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  8.   
  9.   <init-param>  
  10.   
  11.     <param-name>contextConfigLocation</param-name>  
  12.   
  13.     <param-value>/WEB-INF/spring-mvc.xml</param-value>  
  14.   
  15.   </init-param>  
  16.   
  17.   <load-on-startup>1</load-on-startup>  
  18.   
  19. </servlet>  
  20.   
  21. <servlet-mapping>  
  22.   
  23.   <servlet-name>springDispatcherServlet</servlet-name>  
  24.   
  25.   <url-pattern>/</url-pattern>  
  26.   
  27. </servlet-mapping>  

 

 

 

因为Spring MVC主要是用来作为前端控制器,所以它底层自然是Servlet实现的咯,上面配置的意思是配置一个Spring为我们提供好的用来启动Spring MVCServlet,读取指定路径的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是这个样子的:

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.   
  9.     xmlns:context="http://www.springframework.org/schema/context"  
  10.   
  11.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
  12.   
  13.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  14.   
  15.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">  
  16.   
  17.    
  18.   
  19.     <mvc:default-servlet-handler/>  
  20.   
  21. <mvc:annotation-driven></mvc:annotation-driven>  
  22.   
  23.     <context:component-scan base-package="com.cpwl" use-default-filters="false">  
  24.   
  25. </context:component-scan>  
  26.   
  27.    
  28.   
  29. </beans>  

 

 

 

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

 

 

[java] view plain copy
 
  1. package com.cpwl.springtest.controller;  
  2.   
  3.    
  4.   
  5. import org.springframework.stereotype.Controller;  
  6.   
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8.   
  9. import org.springframework.web.bind.annotation.RequestMethod;  
  10.   
  11. import org.springframework.web.servlet.ModelAndView;  
  12.   
  13.    
  14.   
  15. @Controller  
  16.   
  17. public class TestController {  
  18.   
  19.       
  20.   
  21.     public TestController() {  
  22.   
  23.         System.out.println("TestController constructed......");  
  24.   
  25.     }  
  26.   
  27.       
  28.   
  29.     @RequestMapping(value="/test",method=RequestMethod.GET)  
  30.   
  31.     public ModelAndView testMVC(){  
  32.   
  33.         ModelAndView modelAndView = new ModelAndView("/WEB-INF/views/test.jsp");  
  34.   
  35.         modelAndView.addObject("info""陈鹏万里");  
  36.   
  37.         return modelAndView;  
  38.   
  39.     }  
  40.   
  41.    
  42.   
  43. }  

 

 

 

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

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

 

[html] view plain copy
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.   
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4.   
  5. <html>  
  6.   
  7.   <head>  
  8.   
  9.     <title>Spring MVC Test</title>  
  10.   
  11.   </head>  
  12.   
  13.   <body>  
  14.   
  15.     Hello ${info} !!!!!  
  16.   
  17.   </body>  
  18.   
  19. </html>  

 

 

 

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

 

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

 

哎哟,不错哟。

 

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

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

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

怎么解决?两种方案。

 

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

 

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

 

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

 

[html] view plain copy
 
  1. <context:component-scan base-package="com.cpwl"  
  2.   
  3.     use-default-filters="false">  
  4.   
  5.     <context:include-filter type="annotation"  
  6.   
  7.         expression="org.springframework.stereotype.Controller" />  
  8.   
  9.     <context:include-filter type="annotation"  
  10.   
  11.         expression="org.springframework.web.bind.annotation.ControllerAdvice" />  
  12.   
  13. </context:component-scan>  

 

 

 

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

 

[html] view plain copy
 
  1. <context:component-scan base-package="com.cpwl">  
  2.   
  3.     <context:exclude-filter type="annotation"  
  4.   
  5.         expression="org.springframework.stereotype.Controller" />  
  6.   
  7.     <context:exclude-filter type="annotation"  
  8.   
  9.         expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  10.   
  11. </context:component-scan>  

 

 

 

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

 

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

 

 

[html] view plain copy
 
  1. <bean  
  2.   
  3.     class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  4.   
  5.     <property name="prefix" value="/WEB-INF/views/"></property>  
  6.   
  7.     <property name="suffix" value=".jsp"></property>  
  8.   
  9. </bean>  

 

 

 

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

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

 

 

[java] view plain copy
 
  1. @RequestMapping(value="/test",method=RequestMethod.GET)  
  2.   
  3. public String testMVC(Map<String,Object> map){  
  4.   
  5.     map.put("info""陈鹏万里");  
  6.   
  7.     return "test";  
  8.   
  9. }  
原文地址:https://www.cnblogs.com/zhaoyan001/p/7513624.html