spring-mvc - Spring boot 找不到 WebApplicationContext

标签 spring-mvc spring-boot

我有一个简单的 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 应该可以工作

This link is a discussion on how they currently handle @WebFilter how they decided to handle @WebFilter and they also discuss SpringBootServletInitializer and how this would pick process each item twice if both were to be used. Also links to the commits that implement the new feature.

https://github.com/spring-projects/spring-boot/issues/2290



如果您打算将您的应用程序部署为一个 war 文件,您还可以让您的主要配置扩展 SpringBootServletInitializer

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

关于spring-mvc - Spring boot 找不到 WebApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486219/

相关文章:

spring - 在 Spring MVC 中从 Controller 中查找语言环境

java - 使用 Spring MVC 的 XML View

java - 将 Spring 参数添加到 VSCode Debug launch.json

java - 如何在Spring中直接获取最高排序的bean而无需注入(inject)列表?

java - Spring 获取 ServletContext 并将其作为 Bean 提供

java - Spring MVC 3 I18N/MessageSource 不工作

java - 从 Java 中的外部文件读取垃圾收集 (GC) 配置 | Spring Boot

java - 如何为静态 void 方法实现 doNothing() ?

java - 在另一个 Controller 中重用 Spring 服务 Controller 功能

java - 微服务未从 : http://localhost:8888 Springboot 处的服务器获取配置