Spring mvc Spring启动未找到WebApplicationContext
我有一个简单的spring boot应用程序,我正在尝试启动并运行它。配置由一个应用程序上下文(applicationContext.xml)xml和一堆bean组成。我有一个Spring应用程序类:Spring mvc Spring启动未找到WebApplicationContext,spring-mvc,spring-boot,Spring Mvc,Spring Boot,我有一个简单的spring boot应用程序,我正在尝试启动并运行它。配置由一个应用程序上下文(applicationContext.xml)xml和一堆bean组成。我有一个Spring应用程序类: @SpringBootApplication @Configuration @ImportResource("classpath:applicationContext.xml") public class WebCheckApplication { private static fina
@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。所需要的只是让应用程序类extend
SpringBootServletInitializer
大于或等于1.3.0.RC1使用@ServletComponentScan
@ServletComponentScan // <-- This scans for EJB @WebFilter, @WebListener and @WebServlet
@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class WebCheckApplication {
public class WebCheckApplication extends SpringBootServletInitializer {
War部署扩展SpringBootServletInitializer
public class WebCheckApplication extends SpringBootServletInitializer {
在1.3.0.RC1中添加了@ServletComponentScan,因此只需对主应用程序配置进行注释,就可以获取这些配置。否则,将@Component添加到ServletContextListener应该可以工作
此链接讨论了他们当前如何处理@WebFilter,他们决定如何处理@WebFilter,他们还讨论了SpringBootServletilizer,以及如果同时使用这两个选项,将如何选择处理每个项目两次。还链接到实现新功能的提交
如果您打算将应用程序部署为war文件,您还可以让主配置扩展SpringBootServletilizer
当您删除/@WebListener并在WebCheckApplication中将其作为/@Bean注释的方法时会发生什么。另外,顺便说一句,/@springbootplication已经包含了/@配置,这有点违背了我想要导入一个已经完全编写好的外部应用程序上下文XML的目的,对吗?