java - 如何启动一个可运行程序?

标签 java spring spring-mvc scheduled-tasks

我需要从实现 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 - 如何启动一个可运行程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19712192/

相关文章:

Java多线程: how does thread creation with Runnable interface work?

java - Heroku 无法部署 Java 11 Spring Boot App

java - Jersey 2 + Spring 4 + jetty-maven-plugin

spring - WebFilter bean 在安全的 Spring Boot Webflux 应用程序中调用了两次

java - 如何使用 Spring MVC 正确记录 http 请求

java - Spring mongodb 异常 HTTP Status 500 - 过滤器执行抛出异常

java - 了解模型解析的工作原理

java - 安装更新 SSL 证书会破坏 Java Web 集成吗?

java - Spring 映射 Url 到 Controller 和 View

java - 带分页的 API 的 Stream 与 Iterator