java - 使用 spring mvc 安排任务

标签 java spring spring-mvc scheduled-tasks quartz-scheduler

我想在 spring mvc 项目中的每个特定时间运行以下方法,它工作正常并打印第一个输出 但它不访问数据库,因此不显示列表

方法

 public class ScheduleService {

@Autowired
private UserDetailService userDetailService;

public void performService() throws IOException {
    System.out.println("first output");
    List<UserDetail> list=userDetailService.getAll();
    System.out.println(list);

}

配置文件

 <!-- Spring's scheduling support -->
<task:scheduled-tasks scheduler="taskScheduler">
   <task:scheduled ref="ScheduleService" method="performService" fixed-delay="2000"/>
</task:scheduled-tasks>

<!-- The bean that does the actual work -->
<bean id="ScheduleService" class="com.ctbllc.ctb.scheduling.ScheduleService" />

<!-- Defines a ThreadPoolTaskScheduler instance with configurable pool size. -->
<task:scheduler id="taskScheduler" pool-size="1"/>      

最佳答案

尝试这个(并从 xml 文件中删除 bean 定义):

@Component
public class ScheduleService {

    @Autowired
    private UserDetailService userDetailService;

    @Scheduled(fixedDelay = 2000L) // in msec
    public void performService() throws IOException {
        System.out.println("first output");
        List<UserDetail> list=userDetailService.getAll();
        System.out.println(list);

    }

}

关于java - 使用 spring mvc 安排任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23974619/

相关文章:

java - 非公共(public)构造函数导致 Tomcat7 出现问题?

java - 如何使用WebApplicationInitializer依赖项目?

Spring:正确设置@ComponentScan

java - 错误消息的顺序不正确

java - 建立下载本地文件的链接

java - 实现 HttpSession 时的弃用警告

java - 如何正确管理单线程执行?

spring-boot - 如何在spring-data Elasticsearch 中为嵌套对象过滤创建搜索查询?

java - @Test 方法无法注入(inject) TestNG 类中的多个类

Java 设计 : Type Class vs. 类扩展