[英]using @postconstruct and @Scheduled annotation together
I am new learner and using spring annotation for configuration Can i use both @PostConstruct and @Scheduled(fixedRate = 60L * 1000L) on same method as given below ? 我是新手,并且使用spring批注进行配置是否可以按以下相同方法使用@PostConstruct和@Scheduled(fixedRate = 60L * 1000L)? if yes what should be the annotation to class ?
如果是,应该为类添加什么注释?
@Component
public class Cache {
@PostConstruct
@Scheduled(fixedRate = 60L * 1000L)
public void refreshCache() {
...
}
}
Yes, your annotations in the class are correct. 是的,您在课堂上的注释是正确的。 But you better use:
但您最好使用:
@Scheduled(fixedRate = 60L * 1000L, initialDelay=0)
public void refreshCache() {
without the @PostConstruct
because: 没有
@PostConstruct
原因是:
@PostConstruct
. @PostConstruct
进行注释。 @PostConstruct
. @PostConstruct
从方法中引发检查的异常。 There are more causes but I stop here. 还有更多原因,但我在这里止步。
If you do not use any xml, this example should be what you want, which actually is a spring boot application. 如果您不使用任何xml,则此示例应该是您想要的,它实际上是一个Spring Boot应用程序。 https://github.com/soiff-spring/spring-boot-example
https://github.com/soiff-spring/spring-boot-example
My full example is here: https://github.com/soiff-spring/spring-mvc-example 我的完整示例在这里: https : //github.com/soiff-spring/spring-mvc-example
Please pay attention to following file and class: 请注意以下文件和类:
hello-servlet.xml
HelloScheduler
Packaing this project and put it in your tomcat container and start your tomcat, you'll see the log as following: 包装此项目并将其放入tomcat容器并启动tomcat,您将看到如下日志:
20:06:53.003 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594013001 : hello world ...
20:06:54.001 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594014001 : hello world ...
20:06:55.001 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594015001 : hello world ...
20:06:56.002 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594016002 : hello world ...
20:06:57.000 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594017000 : hello world ...
20:06:58.002 [pool-1-thread-1] INFO xyz.cloorc.example.springmvc.HelloScheduler - 1480594018002 : hello world ...
Enjoy yourself. 玩的开心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.