Unable to use Repository class of spring data jpa on a class method in Quartz why?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 19:32:49

Going through the following link http://codrspace.com/Khovansa/spring-quartz-with-a-database/ should help.

Quoting from the above link,

Quartz creates a new job instance on each invocation. It means that Quartz jobs are not regular Spring beans and we can't expect the Spring magic to take an effect automatically (and Spring's 'JobDetailFactoryBean' is not smart enough to do it for us). So we'll have to implement our own job factory that would overwrite the default SpringBeanJobFactory.

So you need to have a custom SpringBeanJobFactory by extending SpringBeanJobFactory & implementing ApplicationContextAware and finally invoke beanFactory.autowireBean(job)

Lets open the discussion here now. And expect all experts guidance/ help here.

In the above example, I modified JobA.java class to get an instance of CustomerRepository repository instance using Constructor injection like below:

@Service
public class JobA extends QuartzJobBean {

    private CustomerRepository customerRepository = null;

    @Autowired
    public JobA(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
        getCustomerList();
    }
    public JobA() { }

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("~~~~~~~ Job A is runing ~~~~~~~~");
    }

    private List<Customer> getCustomerList(){
        List<Customer> customers = customerRepository.findAll();
        for (Customer customer : customers) {
            System.out.println("------------------------------");
            System.out.println("ID : "+customer.getId());
            System.out.println("NAME : "+customer.getName());
            System.out.println("STATUS : "+customer.getStatus());
        }
        return customers;
    }
}

But the problem is that when we use customerRepository instance in the executeInternal() method, it nullify it why ? Why ? If somehow instance wont get nullify we are done !!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!