Analysis of the problem that the java timer cannot be automatically injected (the original Spring timer can be injected into the service in this way), the timer spring

Source: Internet
Author: User

Analysis of the problem that the java timer cannot be automatically injected (the original Spring timer can be injected into the service in this way), the timer spring

In recent days, we have been working on a project and need to use spring timing tasks in the project. However, when operating on the database, we find that data insertion is always not correct. We can see that: because the service layer cannot be injected to the action through common methods, the service layer cannot be executed to perform database operations.

Solution: configure a service injection tool class. The Code is as follows:

I. Tools

public class ApplicationContextUtil implements ApplicationContextAware {          private static ApplicationContext applicationContext;          public static ApplicationContext getApplicationContext() {              return applicationContext;          }          public void setApplicationContext(ApplicationContext applicationContext) {              ApplicationContextUtil.applicationContext = applicationContext;          }          public static Object getBean(String beanName) {              return applicationContext.getBean(beanName);          }      }
Ii. Configure bean
<bean  id ="applicationContextUtil"  class ="org.sihai.soil.util.ApplicationContextUtil" ></bean >
3. Use the ApplicationContextUtil class in the timer to obtain the service.
realmEbi = (RealmEbi) ApplicationContextUtil.getBean("realmEbi");        realmEbi.insertHour(RealmApplianceModel.sendData3);

The solution is perfect, so that we can get the service object for corresponding business processing, without the need to create jdbc for operation.

Java Learning Group 669823128

Related Article

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.