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)