我需要在部署战争时启动一个守护进程。守护进程本身使用应该用 Spring 注入的对象。我做了以下事情:
在 web.xml 中
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>example.AppListener</listener-class>
</listener>
应用监听器.java
public class AppListener implements ServletContextListener {
...
@Override
public void contextInitialized(final ServletContextEvent sce) {
log.info("======================= Begin context init =======================");
try {
// final ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/springapp-servlet.xml");
final ApplicationContext context = new ClassPathXmlApplicationContext("src/main/webapp/WEB-INF/springapp-servlet.xml");
//final ApplicationContext context = new ClassPathXmlApplicationContext("//Users/.../WEB-INF/springapp-servlet.xml");
final SessionServiceDaemon sessionServiceDaemon = context.getBean(SessionServiceDaemon.class);
sessionServiceDaemon.start();
} catch (final Exception e) {
log.error("Was not able to start daemon",e);
}
}
SessionServiceDaemon.java
@Service
@Singleton
public class SessionServiceDaemon {
private final static Logger log = LoggerFactory.getLogger(SessionServiceDaemon.class);
private final SessionServiceHandler handler;
@Inject
public SessionServiceDaemon(final SessionServiceHandler handler) {
log.info("+++++++++++++++++++++++++++++++ SessionServiceDaemon injected");
this.handler = handler;
}
我的 springapp-servlet.xml 只包含注入所需的包:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
<context:component-scan base-package="example" />
<mvc:annotation-driven />
</beans>
在启动日志中,我看到了预期:
+++++++++++++++++++++++++++++++ SessionServiceDaemon injected
其次是
======================= Begin context init =======================
问题是:然后我得到一个异常,无论我使用哪个路径指向 springapp-servlet.xml,该文件都不存在:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/webapp/WEB-INF/springapp-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/webapp/WEB-INF/springapp-servlet.xml] cannot be opened because it does not exist
我尝试了不同的相对路径,甚至是绝对路径都没有成功。我什至编辑了上面的代码,并在我尝试加载上下文的上方添加了以下内容:
try {
log.info(org.apache.commons.io.FileUtils.readFileToString(new File("src/main/webapp/WEB-INF/springapp-servlet.xml")));
} catch (final Exception e) {
log.error("Unable to find file",e);
}
并且很好地打印了 springapp-servlet.xml 的内容。
我的2个问题:
- 当我能够使用与同样的方法?
- 无论如何,我是否有正确的方法来启动具有注入依赖项的守护程序?
PS:我使用Tomcat。