Spring boot 找不到 WebApplicationContext

ale*_*cci 5 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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个 @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());
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试启动应用程序时,出现以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

它发生在这一行:

SysPropsDataAccess = (SystemPropertiesDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("SystemPropertiesDataAccess");
Run Code Online (Sandbox Code Playgroud)

看起来 Spring 没有创建 WebApplicationContext。

ale*_*cci 0

所需要的只是使 Application 类扩展SpringBootServletInitializer