1

我有一个在服务器启动中创建的类。它用作客户端应用程序的入口方法。我无法改变这种行为。

虽然我想@Autowired在这个非托管类中使用 Spring。我读到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 {
}
4

1 回答 1

1

您必须确保在ContextLoaderListener调用之前MyContextListener调用了。

初始化spring容器,ContextLoaderListener加载时编织器只能注入TestServicespring容器初始化的依赖项。

于 2014-01-28T14:14:36.020 回答