2

我需要从实现的类中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>

4

2 回答 2

2

添加<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
    ">
于 2013-10-31T16:53:41.267 回答
0

对于 bean MyClass 添加完整的包 id="myClass" class ="your.package.name.MyClass"。并且需要扫描所有这些注释以便检测并尝试添加 @Component("myClass")

于 2013-10-31T16:56:49.523 回答