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.