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