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 ? 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
. 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. https://github.com/soiff-spring/spring-boot-example
My full example is here: 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.