我有一個簡單的彈簧啓動應用程序,我試圖啓動並運行。該配置由一個應用上下文(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。
當您刪除,會發生什麼/ @ WebListener並在WebCheckApplication中將它作爲一個/ @ Bean註釋方法。 http://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-servlet-containers.html – Zergleb
也btw/@ SpringBootApplication已經包含/ @配置https://github.com/ spring-projects/spring-boot/blob/6193b640a4ba69ee0ca43faf440a6e34acf3dfaf/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java – Zergleb
這種做法違背了我希望導入外部應用程序的目的上下文XML已經完全創作,對嗎? –