Question

I am using autowiring in one of my projects. @autowiring is working fine in the controller, but I need to use same autowiring object in some other class, which is used as task class for quartz. Autowiring is not working there.

I tried this code listed below, but it did not succeed. In all attempts it's getting null for readXmlDao.

@Repository("updateTeam")
public class TeamUpdate {
    @Autowired
    @Qualifier("readXmlDao")
    ReadXmlDao readXmlDao;

Please suggest a solution, thanks.

Was it helpful?

Solution

Autowire works in spring context (class instances managed by spring). Quartz creates its own contexts (creates its own instances). Spring do not have to know about these classes and its why autowires not working on them.

It is more spring/quartz configuration issue, than class annotation issue.

OTHER TIPS

Spring will never leave an @Autowired target null. If it's processing the bean and can't find a match it will throw exceptions.

Since you are telling us it's null, the only possibility is that Spring is not managing your object. You're either creating a TeamUpdate object manually or some other process (not Spring) is creating it for you.

In your spring-servlet file make sure you are scanning the correct packages.

<context:component-scan base-package="your package here"/>

A lot of times you might have your controllers and your repositories in different packages. Make sure to scan both.

you might want to try this approach for using spring managed beans in a non-spring managed object.

http://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top