The Java timer does not automatically inject the problem resolution (originally spring timer can inject service like this)

Source: Internet
Author: User
Tags lua

In recent days, in a project, you need to use spring's scheduled tasks in the project, but in the operation of the database is always unable to correctly insert the data, after viewing only found: Because the service layer can not be injected into the action by ordinary methods, Therefore, it is not possible to execute the service layer and then perform the operation of the database.

Workaround: At this point you need to configure a service Injection tool class with the following code:

First, the tool class

PublicClassApplicationcontextutilImplementsApplicationcontextaware {Privatestatic 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) ; } }
Second, configure the Bean
<bean  id ="applicationcontextutil"  class ="Org.sihai.soil.util.ApplicationContextUtil" ></beans >    
Third, the timer through the Applicationcontextutil class, get service.
Realmebi = (Realmebi) applicationcontextutil.getbean ("Realmebi");        Realmebi.inserthour (REALMAPPLIANCEMODEL.SENDDATA3);

The perfect solution, so that we can get to the service object to do the corresponding business processing, and no need to create JDBC to operate.

Java Learning Group 669823128

The Java timer does not automatically inject the problem resolution (originally spring timer can inject service like this)

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.