2011-02-15 27 views
5

(我不知道,如果这个问题适用于Java EE的应用程序一般或特定于WebSphere的。)如何防止在Java EE应用程序无法启动时,春DI失败

当我们拿到一个Spring DI故障在我们已部署到WebSphere的应用程序(例如,JNDI查找失败)上,应用程序似乎仍然成功启动。

[15/02/11 17:21:22:495 GMT] 00000037 ContextLoader E org.springframework.web.context.ContextLoader initWebApplicationContext Context initialization failed 
           org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mybean' defined in 
    ...big stack trace... 
[15/02/11 17:21:22:526 GMT] 00000037 ApplicationMg A WSVR0221I: Application started: myapp 

如何在春季初始化期间抛出异常时使应用程序无法启动?

+0

例如,在Tomcat 6.0中,如果无法建立弹簧上下文,则应用程序将不会启动。 – Ralph 2011-02-15 18:50:03

回答

2

检查this是否有帮助。基于此,我猜想它是特定于应用程序服务器的,但不确定。

0

绑定Spring应用程序生命周期的生命周期应该有所帮助。

内部J2EE服务器Spring上下文主要通过org.springframework.context.access.ContextSingletonBeanFactoryLocator获取(例如,它由org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor使用)。急于在应用程序启动时调用Spring上下文初始化应该完成这项工作。

它可以使用启动Bean在WebSphere特定的方式来完成:

 

@RemoteHome(AppStartUpHome.class) 
@Stateless 
public class SpringLifecycleBean { 
    private static Log logger = LogFactory.getLog(SpringLifecycleBean.class); 
    private static BeanFactoryReference bfr; 

    public boolean start() throws RemoteException { 
     logger.debug("Initializing spring context."); 

     try { 
      BeanFactoryLocator bfl = ContextSingletonBeanFactoryLocator.getInstance(); 
      //hardcoded spring context's name (refactor for more complex use cases) 
      bfr = bfl.useBeanFactory("appContext"); 
     } catch (Exception e) { 
      logger.error("Spring context startup failed", e); 
      return false; 
     } 

     return true; 
    } 

    public void stop() throws RemoteException { 
     if (bfr != null) { 
      logger.debug("Releasing spring context."); 
      bfr.release(); 
     } 
    } 

} 
 

添加含有类似的代码也将工作javax.servlet.ServletContextListener web应用程序模块。

相关问题