Solve the problem that Spring+quartz cannot inject beans

Source: Internet
Author: User
Tags getmessage

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

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.