Spring配置文件被加载两次问题

来源:互联网 发布:端口映射软件手机版 编辑:程序博客网 时间:2024/05/16 18:19

问题描述

今天在用spring quartz做定时任务的时候,用到了一个在程序启动时加载的配置方法init-method="loadJobInit",用来初始化在数据库中已经保存的定时任务,但是在程序启动之后发现该方法每个任务被执行了两次。

后来经过测试发现自己的Spring配置文件被加载了两次。

06-Dec-2016 14:43:16.311 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]06-Dec-2016 14:43:16.866 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'06-Dec-2016 14:43:16.955 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.register Mapped "{[/test]}" onto public java.lang.String com.test.TestControl.test()06-Dec-2016 14:43:17.052 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Dec 06 14:43:16 CST 2016]; root of context hierarchy06-Dec-2016 14:43:17.142 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Dec 06 14:43:16 CST 2016]; root of context hierarchy06-Dec-2016 14:43:17.237 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 1104 ms06-Dec-2016 14:43:17.248 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started06-Dec-2016 14:43:17.252 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Dec 06 14:43:17 CST 2016]; parent: Root WebApplicationContext06-Dec-2016 14:43:17.253 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]


解决方法

之前我的web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

原来<listener>和<servlet>中的配置都是用来加载Spring配置的,一种是基于Servlet2.3版本中新引入的Listener接口实现,而另一种是基于Servlet接口实现。

<context-para>用来指定文件位置。

之前一直以为<listener>是用来监听@RequestMapping注解的。两个功能相同,在删除了<listener>注解之后问题就解决了。


参考资料:http://www.tmser.com/post-116.html

1 1