Java Spring计划注释它是如何工作的
我已经用java创建了一个函数。该函数应该每天午夜运行Java Spring计划注释它是如何工作的,java,spring,spring-mvc,quartz-scheduler,spring-scheduled,Java,Spring,Spring Mvc,Quartz Scheduler,Spring Scheduled,我已经用java创建了一个函数。该函数应该每天午夜运行 //My function this function is within UpdateService Class @Scheduled(cron = "0 0 0 * * ?") public static void UpdateFn() { try { System.out.println("-----------Background Task Running----------------");
//My function this function is within UpdateService Class
@Scheduled(cron = "0 0 0 * * ?")
public static void UpdateFn() {
try {
System.out.println("-----------Background Task Running----------------");
//code to update some data every day
System.out.println("-----------Background Task Ending----------------");
} catch (Exception e) {
e.printStackTrace();
}
}
//My xml configuration
<task:annotation-driven />
<bean id="UpdateTask" class="com.ss.utility.UpdateService"></bean>
</beans>
//我的函数此函数在UpdateService类中
@已计划(cron=“0**?”)
公共静态void UpdateFn(){
试一试{
System.out.println(“--------------后台任务运行-----------------”;
//每天更新一些数据的代码
System.out.println(“--------------后台任务结束-----------------”;
}捕获(例外e){
e、 printStackTrace();
}
}
//我的xml配置
但我没有按预期工作。有时执行,有时不执行。对此有任何解决方案
Spring版本是4您不应该为此使用静态方法。请尝试使用以下代码:
@Scheduled(cron = "0 0 0 * * ?")
public void UpdateFn() {
try {
System.out.println("-----------Background Task Running----------------");
//code to update some data every day
System.out.println("-----------Background Task Ending----------------");
} catch (Exception e) {
e.printStackTrace();
}
}
它在静态方法上不起作用。谢谢。我会修改并检查它