The null pointer problem occurs when the bean is not injected with the quartz development schedule

Source: Internet
Author: User
Tags getmessage locale

When you develop a job class, @Autowired annotations do not work and a null pointer exception occurs:

Reason: Because @autowired is injecting attributes into the job class instance of spring injection. In our job class, when the method executes, quartz instantiates a jobclass, which is different from the instance of spring injection, so there is no attribute and the property is empty.

On the internet to find a lot of methods are said to Springbeanjobfactory, I tried, no use.

There is a way to use it, rewrite applicationcontextaware, and get the bean in the job's execute (), which is implemented as follows:

Package Com.operation.quartzManger;

Import Java.util.Locale;

Import Com.operation.mapper.SystemView;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

/**
 * System Bean help 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 ());
	}
To inject a help class into spring

<ID= "springcontextutilclass=" Com.operation.quartzManger.SpringContextUtil "></Bean>
The execution method in the job class is called:

SystemView systemview= (SystemView) Springcontextutil.getbean ("SystemView");
Systemview.updatesysstatusbyid (Sysuuid,"1","System normal");
The test system is normal.





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.