Должен помочь жизненный цикл Binding контекста Spring с жизненным циклом приложения.
Внутри J2EE-сервера контекст Spring в основном получается через org.springframework.context.access.ContextSingletonBeanFactoryLocator (например, он используется org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor).Выполнение инициализации контекста Spring с нетерпением при запуске приложения должно сделать эту работу.
Это можно сделать особым образом для WebSphere, используя Startup Beans:
@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();
}
}
}
Добавление модуля webapp с javax.servlet.ServletContextListener, содержащий аналогичный код, также будет работать.