Do the project when met a problem, in fact, have encountered similar, although the last with new Classpathxmlapplicationcontext way to solve, but also a palliative.
Meet again today, summarize the reasons and the current feel better solution.
Situation:
First I created the job through the Quartz factory factory in spring, which is actually a dispatch manager that dynamically reads the database information and then creates and manages more jobs.
<bean id= "Springqtzjobmethod"
class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<property name=" TargetObject ">
<ref bean=" Springqtzjob "/>
</property>
<property name=" Targetmethod ">
<!--the method name you need to perform--
<value>init</value>
</property>
</bean >
When I created the job again in Java, I found that all the beans in my class that implemented the job interface were not injected.
Reason:
I create the job class by reflection (implemented in code) instead of using spring to create (not an instance in the spring container), so I get no beans.
In fact, there is a way to configure, but also reflect the way to create:
<bean name= "Initjobdetail" class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" >
< Property Name= "Jobclass" value= "Com.xxx.services.InitJob"/>
</bean>
Solution:
The job implementation creates a task instance each time, and if you do new Classpathxmlapplicationcontext ("Classpath*:xxx") in the job, the above situation can be resolved, but the job creates an instance each time. In this way, in high concurrency or long-time use, it is easy to memory overflow;
Therefore, a better solution is to add the Applicationcontextaware implementation class Springcontextutil to the spring configuration file, Let spring do a service or DAO bean initialization at initialization time, so that the util can be used to get the initialized instance in the job, instead of creating the context each time.
Package com.framework.util;
Import Java.util.Locale;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;
/**
* System Bean Helper class */public
class Springcontextutil implements Applicationcontextaware {
private static ApplicationContext context;
@Override
@SuppressWarnings ("static-access") public
void Setapplicationcontext (ApplicationContext Contex)
throws beansexception {
//TODO auto-generated method stub
this.context = Contex;
}
public static Object Getbean (String beanname) {
return Context.getbean (beanname);
}
public static string GetMessage (String key) {
return context.getmessage (key, NULL, Locale.getdefault ());
}
}
Add this util to the spring configuration
<bean id= "Springcontextutil" class= "Com.framework.util.SpringContextUtil" ></bean>
Called when this is done
Tfuserchargeservice Tfuserchargeservice = (tfuserchargeservice) springcontextutil.getbean ("TfUserChargeService");
List<tfproductmonthcharge> chargelist= Tfuserchargeservice.countproductmonthcharge (lastMonth);
......
I've also written a summary of how the bean in spring is obtained, connecting http://blog.csdn.net/a714832876/article/details/47661003