use*_*882 19 spring scheduling spring-mvc
我的Spring基于注释的任务调度程序有问题 - 我无法使其工作,我在这里看不到任何问题...
应用程序的context.xml
<task:scheduler id="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
Run Code Online (Sandbox Code Playgroud)
豆
@Service
public final class SchedulingTest {
private static final Logger logger = Logger.getLogger(SchedulingTest.class);
@Scheduled(fixedRate = 1000)
public void test() {
logger.debug(">>> Scheduled test service <<<");
}
}
Run Code Online (Sandbox Code Playgroud)
ahl*_*hll 40
Spring @Configuration(非xml配置)用于注释驱动的任务
只需在WebMvcConfig类上添加@EnableScheduling即可
@Configuration
@EnableWebMvc
@EnableAsync
@EnableScheduling
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/** Annotations config Stuff ... **/
}
Run Code Online (Sandbox Code Playgroud)
Ser*_*uşu 23
如果您想使用task:annotation-driven方法并且@Scheduled注释不起作用,那么您很可能context:component-scan在上下文中错过了xml.如果没有这一行,spring就无法猜测在哪里搜索注释.
<context:component-scan base-package="..." />
Run Code Online (Sandbox Code Playgroud)
Bab*_*yan 12
发生这种情况是因为默认情况下 Spring 延迟初始化 bean。
通过放置此注释禁用 bean 的延迟初始化
@Lazy(false)
Run Code Online (Sandbox Code Playgroud)
在您的@Component.
我终于找到了解决办法。
应用程序上下文.xml
<bean id="schedulingTest" class="...SchedulingTest" />
<task:scheduled-tasks>
<task:scheduled ref="schedulingTest" method="test" cron="* * * * * ?"/>
</task:scheduled-tasks>
Run Code Online (Sandbox Code Playgroud)
以及test()没有注释的方法。该方法每秒运行一次并且运行良好。
| 归档时间: |
|
| 查看次数: |
36774 次 |
| 最近记录: |