How to autowire classes in unmanaged instances?

I have a class that gets created within server startup. It serves as entry method for the client application. I cannot change this behaviour.

Though I'd like to use Spring @Autowired inside this unmanaged class. I read that aspectj weaving might be the way to go. It seems to already execute accoding to logs:

2014-01-28 13:11:10,156 INFO org.springframework.context.weaving.DefaultContextLoadTimeWeaver: Using a reflective load-time weaver for class loader: org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader

But still my injected Dao service is null. What might be missing?

@WebListener
public class MyContextListener implements ServletContextListener {
        @Override
            public void contextInitialized(ServletContextEvent sce) {
            new TestService().run(); //throws NPE
        }
}

@Configurable
class TestService extends AbstractLegacyService {       
    @Autowired
    private Dao dao;

    @Override
    public void run() {
        //dao is always null
        dao.find();
    }
}


@Component
class Doa dao;

enable weaving (tomcat):

@Configuration
@EnableLoadTimeWeaving
@EnableSpringConfigured
public class AppConfig {
}

Answers


You must make sure that the ContextLoaderListener is invoked before the MyContextListener gets invoked.

The ContextLoaderListener initializes the spring container and the load-time weaver can only inject the dependencies of the TestService if the spring container is initialized.


Need Your Help

Is there a specific reason same-named properties and methods are disallowed in classes but allowed as extension methods?

c# compiler-construction properties methods extension-methods

I was thinking of List's functionality and not thinking about the fact that Count() was an extension method when I made the false assumption that I could write a class having a same named property ...

Hadoop Load and Store

hadoop apache-pig hdfs

When I am trying to run a Pig script which has two "store" to the same file this way