Test spring static injection of TimerTask

Source: Internet
Author: User


It is quite simple to use the built-in jdk TimerTask for simple task processing;

When spring cannot be injected, the specific performance is as follows:

@ Resource annotation is not supported on static fields

1. The corresponding dao can be injected at the first startup.

2. When the run method is executed in the middle, dao is NULL.

The solution is to introduce a static initialization to convert:

 
@ Resource
Private BookDao bookDao;
 
Private static BookDao bkDao;
 
Public void start (){
BkDao = this. bookDao;
If (! Start ){
VisitStatServiceUtil daemon = new VisitStatServiceUtil ();
Click_timer = new Timer ("VisitStatServiceUtil", true );
Click_timer.schedule (daemon, INTERVAL, INTERVAL); // The INTERVAL between running and running is 1 minute.
Start = true;
    }
Log.info ("VisitStatService started .");
}

Note: You can use get set to enable xml configuration to take effect. (a series of problems caused by removing the DAO and Service xml configuration files from the project are broken one by one to switch to the annotation mode)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.