2015-11-02 116 views
2

我有一個簡單的彈簧啓動應用程序,我試圖啓動並運行。該配置由一個應用上下文(applicationContext.xml)和一堆bean組成。我有一個Spring應用程序類:Spring boot No WebApplicationContext found

@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); 
      } 
     } 
    } 
} 

而且我有一個從ServletContext中抓住從WebContext幾豆@WebListener類:

@WebListener 
public class MojoSystemPropertiesContextInitializer extends MojoSysPropsAlertsFetcher implements ServletContextListener { 

    private static final Logger logger = Logger.getLogger(MojoSystemPropertiesContextInitializer.class); 

    @Override 
    public void contextDestroyed(ServletContextEvent sce) { 
     //remove the MojoSystemProperties and alert types map object from context 
     sce.getServletContext().removeAttribute(BaseAuthenticatedController.MOJOSYSPROPS_KEY); 
     sce.getServletContext().removeAttribute(BaseAuthenticatedController.ALERT_TYPES_MAP_KEY); 
    } 

    @Override 
    public void contextInitialized(ServletContextEvent sce) { 

     mojoSysPropsDataAccess = (MojoSystemPropertiesDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("MojoSystemPropertiesDataAccess"); 
     mojoAlertsDataAccess = (MojoAlertDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("AlertsDataAccess"); 
     fetchObjects(sce.getServletContext()); 
    } 
} 

當我嘗試啓動應用程序,我出現以下錯誤:

SEVERE: Exception sending context initialized event to listener instance of class mojo.web.MojoSystemPropertiesContextInitializer 
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 
    at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:83) 
    at mojo.web.MojoSystemPropertiesContextInitializer.contextInitialized(MojoSystemPropertiesContextInitializer.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) 

,它發生在這一行:

mojoSysPropsDataAccess = (MojoSystemPropertiesDataAccess) WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getBean("MojoSystemPropertiesDataAccess"); 

看起來Spring並沒有創建WebApplicationContext。

+0

當您刪除,會發生什麼/ @ WebListener並在WebCheckApplication中將它作爲一個/ @ Bean註釋方法。 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html – Zergleb

+0

也btw/@ SpringBootApplication已經包含/ @配置https://github.com/ spring-projects/spring-boot/blob/6193b640a4ba69ee0ca43faf440a6e34acf3dfaf/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java – Zergleb

+0

這種做法違背了我希望導入外部應用程序的目的上下文XML已經完全創作,對嗎? –

回答

0

所有這一切都需要的是讓應用程序類擴展SpringBootServletInitializer

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 { 

戰爭部署擴展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