我有一个在服务器启动中创建的类。它用作客户端应用程序的入口方法。我不能改变这种行为。
不过,我想在这个非托管类中使用Spring@Autowired。我读到aspectj编织可能是最好的选择。它似乎已经执行了对日志的编码:
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但我注入的Dao服务仍然是null。可能少了什么?
@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;启用编织(tomcat):
@Configuration
@EnableLoadTimeWeaving
@EnableSpringConfigured
public class AppConfig {
}发布于 2014-01-28 14:14:36
必须确保在调用ContextLoaderListener之前调用MyContextListener。
ContextLoaderListener初始化弹簧容器,加载时间编织器只能在春容器初始化时注入TestService的依赖项。
https://stackoverflow.com/questions/21406091
复制相似问题