简体   繁体   中英

how to inject a service in a thread created through executor service in spring

I have created a simple service like ... where I have to take some data from database at later

package com.spring.scheduler.example.springscheduler;

import org.springframework.stereotype.Service;

@Service
public class ExampleService {

    private String serviceName;
    private String repository;

    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public String getRepository() {
        return repository;
    }
    public void setRepository(String repository) {
        this.repository = repository;
    }
}

Here is my task scheduler created to run different threads.

@Component
public class TaskSchedulerService {

    @Autowired 
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
     public ScheduledFuture<?> job1;        
     public ScheduledFuture<?> job2;

     @Autowired 
    ApplicationContext applicationContext;


     @PostConstruct
     public void job1() {
         //NewDataCollectionThread thread1 = new NewDataCollectionThread();
         NewDataCollectionThread thread1 = applicationContext.getBean(NewDataCollectionThread.class);
         AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
         factory.autowireBean(thread1);
         factory.initializeBean(thread1, null);
         job1 = threadPoolTaskScheduler.scheduleAtFixedRate(thread1, 1000);
     }

 }

This is a thread trying to call from scheduler. I tried to create service instance forcibly by using application context but it's not created.

@Configurable
@Scope("prototype")
public class NewDataCollectionThread implements Runnable {

private static final Logger LOGGER = 
LoggerFactory.getLogger(NewDataCollectionThread.class);

@Autowired
private ExampleService exampleService;

@Override
public void run() {
    LOGGER.info("Called from thread : NewDataCollectionThread");
    System.out.println(Thread.currentThread().getName() + " The Task1 
    executed at " + new Date());
    try {
        exampleService.setRepository("sdasdasd");
        System.out.println("Service Name :: " + 
        exampleService.getServiceName());
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

}

Kindly suggest what are the possible ways to achieve it.

Try something like this:

@Autowired 
private AutowireCapableBeanFactory beanFactory;

@PostConstruct
public void job1() {
    NewDataCollectionThread thread1 = new NewDataCollectionThread();
    beanFactory.autowireBean(thread1);
    job1 = threadPoolTaskScheduler.scheduleAtFixedRate(thread1, 1000);
}

In NewDataCollectionThread I injected the Example service successfully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM