我有一个简单的 Spring Boot 应用程序,我正在尝试启动并运行它。该配置包含一个应用上下文 (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。
最佳答案
大于等于 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 应该可以工作
如果您打算将您的应用程序部署为一个 war 文件,您还可以让您的主要配置扩展 SpringBootServletInitializer
关于spring-mvc - Spring boot 找不到 WebApplicationContext,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33486219/