【发布时间】:2014-09-02 12:35:26
【问题描述】:
我已经阅读了有关在 Spring 上调用两次方法的所有问题,但我无法解决这个问题。
我不时使用注解@Scheduled 运行任务。
@Scheduled(fixedDelay = 60000)
public void getTickets() {
Tomcat 运行 WebApplicationContext
.16:33:00.598 [localhost-startStop-1] INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started
.16:33:00.752 [localhost-startStop-1] INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Fri Jul 11 16:33:00 BRT 2014]; root of context hierarchy
.16:33:00.835 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml] ...
和 servlet 上下文。
.16:33:01.614 [localhost-startStop-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'my-context': initialization started
.16:33:01.620 [localhost-startStop-1] INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'my-context-servlet': startup date [Fri Jul 11 16:33:01 BRT 2014]; parent: Root WebApplicationContext
.16:33:01.621 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/my-context-servlet.xml]
然后我的方法被调用了两次。
我认为有两种选择:
1 - 删除根 WebApplicationContext。
有可能吗?当我删除那段代码时:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我无法从我的网络应用程序中获取任何信息。
GRAVE: Servlet.service() for servlet [my-context] in context with path [] threw exception java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:252)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
2 - 从根上下文中“过滤”类
这样的事情有可能吗?
这是我的完整 web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>my-context</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-context</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
感谢您为解决此问题提供的任何帮助。
谢谢。
【问题讨论】:
-
可能bean被实例化了两次。例如,如果您在上下文(xml 或 java)中声明它,并且您在类上有组件扫描和 @Component 注释。
标签: java spring tomcat spring-mvc