admin管理员组

文章数量:1794759

Web项目中使用XML配置加载Spring及Spring MVC的组件

Web项目中使用XML配置加载Spring及Spring MVC的组件

Spring MVC集成了Spring对象管理、约定大于配置(CoC)、函数式编程的思想以及现有MVC框架的特点于一身,成为Java领域Web项目中最流行的MVC框架。

web.XML

web.xml可以说是Web项目的驱动配置,Spring及Spring MVC的初始化就是写在这里。

下面以spring-mvc-showcase项目的web.xml做示例:

<!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

DispatcherServlet类是Spring MVC的转发控制器,所以需要指明初始化DispatcherServlet类的必要信。此例中是设置contextConfigLocation参数的值为/WEB-INF/spring/appServlet/servlet-context.xml,Spring MVC框架的controller配置、静态资源配置、上传文件配置都是写在这个xml文件中。

servlet-context.xml示例

<mvc:view-controller path="/" view-name="home"/> <context:component-scan base-package="org.springframework.samples.mvc" /> <mvc:annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --> <resources mapping="/resources/**" location="/resources/" />

而范围更大的Spring容器管理的组件的加载则是由ContextLoaderListener完成,通过上下文参数(context-param)配置。

ContextLoaderListener配置

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

此例中是设置contextConfigLocation参数的值为/WEB-INF/spring/root-context.xml。

root-context.xml示例:

<context:annotation-config /> <context:component-scan base-package="com.example" /> <task:annotation-driven /> <!-- 数据源配置 --> <bean id="dataSource"></bean>

DispatcherServlet与ContextLoaderListener共用一个配置文件

很多项目会出现共用一个配置文件的情况,比如:

<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:xml/spring/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>viewspace</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:xml/spring/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>

DispatcherServlet与ContextLoaderListener共用同一个配置文件:classpath:xml/spring/applicationContext.xml。这种方式存在一个隐患,因为同一个配置文件被加载了两次,那么其中的组件、配置就被实例化了两次。由于大部分的组件是以单例的方式实例化的,所以即便是这样,很多时候也不会出现什么问题。

定时任务组件问题

Spring框架对定时任务组件做了封装,并设计了灵活的时间配置。例(注解方式):

@EnableScheduling @Component public class FooTask { @Scheduled(cron = "0 0/60 8-23 * * ?") public void run() { } }定时任务的时间配置采用了 cron表达式,此例中表示在每一天的8:00 am —— 11:00 pm时段每一小时运行一次run()方法。

共用一个配置文件会导致定时任务组件被实例化两次,结果就是这个定时任务会被重复执行两次。

解决办法就是像本文的第一个例子那样,参照spring-mvc-showcase项目的web.xml配置,使用不同的配置文件,DispatcherServlet类使用servlet-context.xml,ContextLoaderListener类使用root-context.xml,并且这两个文件中不能有冲突和重复的配置。

root-context.xml与servlet-context.xml的包含关系

本文标签: 组件加载项目webxml