Java Spring4运行调度任务两次,没有applicationContext.xml
我读了这两本书,但它们似乎都不起作用 我没有@Configurable类,也没有使用Java Spring4运行调度任务两次,没有applicationContext.xml,java,spring,spring-mvc,scheduled-tasks,Java,Spring,Spring Mvc,Scheduled Tasks,我读了这两本书,但它们似乎都不起作用 我没有@Configurable类,也没有使用applicationcontext.xml 我的日程安排任务 @EnableScheduling @Component public class ScheduledTasks { @Autowired private ApplicationContext ctx; @Scheduled(fixedRate=5000) public void monitorRoom(){
applicationcontext.xml
我的日程安排任务
@EnableScheduling
@Component
public class ScheduledTasks {
@Autowired
private ApplicationContext ctx;
@Scheduled(fixedRate=5000)
public void monitorRoom(){
System.out.println("--------------------");
}
}
我可以看到,当我在tomcat中运行应用程序时,任务会在5秒内打印两次
有人说要搬走
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
这是可行的,但我觉得这不是一个解决方案。因为这是必须的。(至少从谷歌的例子中可以看出)
这是我的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>wodinow</display-name>
<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>
沃迪诺
上下文配置位置
/WEB-INF/dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="wodinow.weixin.jaskey" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/pages/**" location="/resources/pages/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:annotation-driven/>
</beans>
/WEB-INF/jsp/
.jsp
删除上下文加载程序侦听器。只有在必须初始化父上下文时才需要它。dispatcher servlet中的上下文是独立的,但如果需要,可以访问此类父上下文,即检索服务或dao。实际上,您使用的是xml文件进行配置,名称并不重要。ContextLoaderListener
和DispatcherServlet
xml都加载同一个文件,这意味着每个bean都会被加载两次,包括预定的bean。简单的解决方案是从配置中删除ContexLoaderListener
。另一件事是选择使用xml或java配置,但不要混用。您有一个@EnableScheduling
,它是基于java的配置,您使用的是xml,因此将
添加到您的xml配置中,而不是该注释。@M.Deinum,谢谢!因此,对于加载dispatcher-servlet.xml的任何情况,我们都不需要使用contextloaderlistener?为什么所有的向导都给我相同的配置?请把你的答案贴在答题纸上好吗?非常感谢。那么,我可以考虑如果我使用Spring MVC,不需要加载上下文吗?