Configuration of Spring Application-quartz
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-4.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
Http://www.springframework.org/schema/util
Http://www.springframework.org/schema/util/spring-util-4.0.xsd ">
<bean id= "Ordersquartz" class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" >
<property name= "name" value= "Examplejob"/>
<property name= "Jobclass" value= "Com.ak.orders.quartz.OrdersQuartz"/>
</bean>
<bean id= "Tasktrigger" class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" >
<property name= "Jobdetail" ref= "Ordersquartz"/>
<property name= "cronexpression" value= "0 */8 * * *?"/>
</bean>
<bean id= "Schedulerfactorybean"
class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref bean= "Tasktrigger"/>
</list>
</property>
</bean>
</beans>
Task class
Import Org.springframework.scheduling.quartz.QuartzJobBean;
public class Ordersquartz extends quartzjobbean{
@Resource
Private OrderService OrderService;
@Override
protected void Executeinternal (Jobexecutioncontext arg0) throws Jobexecutionexception {
Task content
Orderservice.saveorders (orders);
}
}
Problem
Nullpointexception
OrderService is null. The method is not called.
Solve:
1. Add a System helper class
Package Com.ak.orders.quartz;
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 ());
}
}
2. Add a bean to the spring configuration file
<bean id= "Springcontextutil" class= "Com.ak.orders.quartz.SpringContextUtil" ></bean>
3. Program Get Bean Code
Private OrderService OrderService = (orderservice) springcontextutil.getbean ("parms");
Solve the problem that Spring+quartz cannot inject beans