The quartz implementation of the job interface cannot inject the instance bean

Source: Internet
Author: User
Tags getmessage locale

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


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.