gpt4 book ai didi

java - 如何启动一个Runnable?

转载 作者:行者123 更新时间:2023-11-30 09:15:49 25 4
gpt4 key购买 nike

我需要从实现 Runnable 的类打开我的 Java EE 应用程序 7 Spring MVC。我在 servlet.xml 中声明我的类:

<beans:bean class="MyClass" scope="singleton">
</beans:bean>
<annotation-driven />
<context:component-scan base-package="MyClass" />
<context:annotation-config />

这是我的类,它有一个 run() 方法,该方法来自所有 X 分钟,但它不起作用。为什么?

@Scope("singleton")
@Component
public class MyClass implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(myClass.class);

@Scheduled(initialDelay = 1000, fixedDelay = 10000)
@Override
@Transactional
public void run() {
LOG.debug("IN");
}
}

我的 servlet.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<context:annotation-config />
<context:component-scan base-package="com" />

<jpa:repositories base-package="com.dao" />

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources
directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean class="com.MyClass" scope="singleton">
</beans:bean>

最佳答案

添加<task:annotation-driven />给你的servlet.xml

<annotation-driven />还不够

详细文档在这里 http://docs.spring.io/spring/docs/3.0.x/reference/scheduling.html

别忘了xmlns

<bean ....
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"

例如:替换你的 xml 头

<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
">

关于java - 如何启动一个Runnable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com