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
- 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
- Package com.gxjy.job;
- Import Java.util.Map;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import Org.springframework.web.context.ContextLoader;
- Import Org.springframework.web.context.WebApplicationContext;
- Import Com.gxjy.dao.InfoDao;
- Import Com.gxjy.service.InfoService;
- Import Com.gxjy.service.runnable.DudeRunner;
- Public class scrawlerjob{
- @Autowired
- private Infodao Infodao;
- public Void execute () {
- Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();
- map<string, infoservice> map = Wac.getbeansoftype (infoservice. Class);
- For (Infoservice infoService:map.values ()) {
- System.out.println ("Start:" +infoservice.getclass (). GetName ());
- New Thread (new Duderunner (Infoservice, Infodao)). Start ();
- }
- }
- }
Focus on
[Java]View PlainCopy
- 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
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns="Http://www.springframework.org/schema/beans"
- 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.xsd ">
- <Bean id="Job" class="Com.gxjy.job.ScrawlerJob"></bean>
- <Bean id= "jobdetail" class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
- <property name="TargetObject">
- <ref bean="job"/>
- </Property>
- <property name="Targetmethod">
- <value>execute</value>
- </Property>
- </Bean>
- <Bean id= "trigger" class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean ">
- <property name="Jobdetail">
- <ref bean="Jobdetail"/>
- </Property>
- <property name="cronexpression">
- <value>0 0 3 * *? </value>
- </Property>
- </Bean>
- <Bean id= "schedulerfactorybean" class=" Org.springframework.scheduling.quartz.SchedulerFactoryBean ">
- <property name="triggers">
- <list>
- <ref bean="trigger"/>
- </list>
- </Property>
- < name= "autostartup" value="true"> </Property>
- </Bean>
- </Beans>
MAVEN relies on additional spring-context-support dependencies (including support for quartz) in addition to the basic spring and quartz:
[HTML]View PlainCopy
- <pre name="code" class="html"> <dependency>
- <groupId>org.springframework</groupId>
- <artifactid>spring-context-support</artifactid>
- <version>4.2.2.release</version>
- </Dependency>
Get spring's webapplicationcontext or ServletContext in Quartz's job