Get spring's webapplicationcontext or ServletContext in Quartz's job

Source: Internet
Author: User

Sometimes we need to get spring's IOC container in the Timer class in the Web project, Webapplicationcontext, to get all the beans that implement an interface, because @autowired seems to inject only a single bean.

At first I was writing a servletcontextlistener, when starting the server to construct the timer and start, the Webapplicationcontext to the timer job, In Servletcontextlistener this gets webapplicationcontext:

[Java]View PlainCopy
    1. Webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext);


Then call Webapplicationcontext.getbeansoftype (Infoservice.class) in the job to get all the beans that implement the interface.


In fact, can be simpler, less nonsense, this is a Pojo job:


[Java]View PlainCopy
  1. Package com.gxjy.job;
  2. Import Java.util.Map;
  3. Import org.springframework.beans.factory.annotation.Autowired;
  4. Import Org.springframework.web.context.ContextLoader;
  5. Import Org.springframework.web.context.WebApplicationContext;
  6. Import Com.gxjy.dao.InfoDao;
  7. Import Com.gxjy.service.InfoService;
  8. Import Com.gxjy.service.runnable.DudeRunner;
  9. Public class scrawlerjob{
  10. @Autowired
  11. private Infodao Infodao;
  12. public Void execute () {
  13. Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();
  14. map<string, infoservice> map = Wac.getbeansoftype (infoservice.   Class);
  15. For (Infoservice infoService:map.values ()) {
  16. System.out.println ("Start:" +infoservice.getclass (). GetName ());
  17. New Thread (new Duderunner (Infoservice, Infodao)). Start ();
  18. }
  19. }
  20. }


Focus on

[Java]View PlainCopy
    1. Contextloader.getcurrentwebapplicationcontext ();

This can be directly obtained webapplicationcontext, of course, you can further call Getservletcontext () to get to ServletContext.



This is the configuration for quartz in spring:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsd ">
  5. <Bean id="Job" class="Com.gxjy.job.ScrawlerJob"></bean>
  6. <Bean id= "jobdetail" class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
  7. <property name="TargetObject">
  8. <ref bean="job"/>
  9. </Property>
  10. <property name="Targetmethod">
  11. <value>execute</value>
  12. </Property>
  13. </Bean>
  14. <Bean id= "trigger" class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
  15. <property name="Jobdetail">
  16. <ref bean="Jobdetail"/>
  17. </Property>
  18. <property name="cronexpression">
  19. <value>0 0 3 * *? </value>
  20. </Property>
  21. </Bean>
  22. <Bean id= "schedulerfactorybean" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean ">
  23. <property name="triggers">
  24. <list>
  25. <ref bean="trigger"/>
  26. </list>
  27. </Property>
  28. < name= "autostartup" value="true"> </Property>
  29. </Bean>
  30. </Beans>



MAVEN relies on additional spring-context-support dependencies (including support for quartz) in addition to the basic spring and quartz:

[HTML]View PlainCopy
    1. <pre name="code" class="html"> <dependency>
    2. <groupId>org.springframework</groupId>
    3. <artifactid>spring-context-support</artifactid>
    4. <version>4.2.2.release</version>
    5. </Dependency>

Get spring's webapplicationcontext or ServletContext in Quartz's job

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.