我想在 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/