我有一个简单的春季启动应用程序,我正在努力让它运行起来。配置包含一个应用程序上下文(applicationContext.xml)XML,其中包含一堆bean。我有一个Spring应用程序类:
@SpringBootApplication
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class WebCheckApplication {
private static final Logger logger = Logger.getLogger(WebCheckApplication.class);
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(WebCheckApplication.class, args);
if (logger.isDebugEnabled()) {
logger.debug("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
logger.debug(beanName);
}
}
}
}
我有一个@WebListener类,它从ServletContext中抓取WebContext中的一些bean:
@WebListener
public class SystemPropertiesContextInitializer extends SysPropsAlertsFetcher implements ServletContextListener {
private static final Logger logger = Logger.getLogger(SystemPropertiesContextInitializer.class);
@Override
public void contextDestroyed(ServletContextEvent sce) {
//remove the SystemProperties and alert types map object from context
sce.getServletContext().removeAttribute(BaseAuthenticatedController.SYSPROPS_KEY);
sce.getServletContext().removeAttribute(BaseAuthenticatedController.ALERT_TYPES_MAP_KEY);
}
@Override
public void contextInitialized(ServletContextEvent sce) {
SysPropsDataAccess = (SystemPropertiesDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("SystemPropertiesDataAccess");
AlertsDataAccess = (AlertDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("AlertsDataAccess");
fetchObjects(sce.getServletContext());
}
}
当我尝试启动应用时,出现以下错误:
SEVERE: Exception sending context initialized event to listener instance of class web.SystemPropertiesContextInitializer
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:83)
at .web.SystemPropertiesContextInitializer.contextInitialized(SystemPropertiesContextInitializer.java:31)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
它出现在这一行:
SysPropsDataAccess = (SystemPropertiesDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("SystemPropertiesDataAccess");
看起来Spring没有创建WebApplicationContext。
答案 0 :(得分:1)
大于或等于1.3.0.RC1使用@ServletComponentScan
@ServletComponentScan // <-- This scans for EJB @WebFilter, @WebListener and @WebServlet
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class WebCheckApplication {
小于或等于1.2.x使用@Component
来扫描侦听器
@Component // <-- This allows the component to be found by @ComponentScan inside of @SpringBootApplication
@WebListener
public class MojoSystemPropertiesContextInitializer extends MojoSysPropsAlertsFetcher implements ServletContextListener {
War Deploy扩展SpringBootServletInitializer
public class WebCheckApplication extends SpringBootServletInitializer {
在1.3.0.RC1中添加了@ServletComponentScan,因此只需注释您的主应用程序配置就应该允许这些内容被选中。否则,将@Component添加到ServletContextListener应该可以正常工作
这个链接讨论了他们当前如何处理@WebFilter他们如何决定处理@WebFilter,他们还讨论了SpringBootServletInitializer以及如果要使用两个项目,它将如何选择处理每个项目两次。还链接到实现新功能的提交。
如果您打算将应用程序部署为war文件,那么您的主配置也可以扩展为SpringBootServletInitializer
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html
答案 1 :(得分:0)
所需的只是使Application类扩展SpringBootServletInitializer