2

I want to make applicationContext inside scheduler method. it is possible ? )

 <task:annotation-driven executor="executor" scheduler="scheduler"/>
 <task:executor id="executor" pool-size="5"/>
 <task:scheduler id="scheduler" pool-size="10"/>

And my class :

@Scheduled(fixedDelay=100)
public void doSomething() {

}

How me add that ? :

private ApplicationContext applicationContext;
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {

    this.applicationContext = applicationContext; 
}

That use it code :

JDBCEntityDAO obj = (JDBCEntityDAO) applicationContext.getBean("taEntityDAO"); 

Help please

1 Answers1

3

Just add @Autowired or any other dependency injection annotation to your job classes (the ones with @Scheduled - annotated methods). If you need a reference to the ApplicationContext, make your job implement the ApplicationContextAware interface. This will work with the Spring native scheduler. If you use Quartz, you'll have to do a little more work, see here inject bean reference into a Quartz job in Spring? . By the way, you can use @Resourse annotation to inject beans by name.

Community
  • 1
  • 1
Alexander
  • 2,761
  • 1
  • 28
  • 33