扩充ContextLoaderListener类,在WEB 系统启动时将ApplicationContext单例压人BeanFactory中
扩展ContextLoaderListener类,在WEB 系统启动时将ApplicationContext单例压人BeanFactory中
package net.gbicc.x27.util.web; import net.gbicc.BpmConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class NeqBeanFactory { private static final BpmConfig config = BpmConfig.getInstance(); private static final String clientSite = config.getClientSite(); private ApplicationContext context = null; private static NeqBeanFactory inst = new NeqBeanFactory(); private NeqBeanFactory() { }; public static NeqBeanFactory getInstance() { return inst; } public synchronized void setContext(ApplicationContext context) { this.context = context; } public synchronized ApplicationContext getContext() { if (context == null) { bulidContext(); } return context; } /** * 根据beanId 获取Bean * * @param beanId * @return */ public synchronized Object getBean(String beanId) { if (context == null) { bulidContext(); System.out.println("NeqBeanFactory===bulidContext===beanId="+beanId); } System.out.println("NeqBeanFactory====NOT_bulidContext===beanId="+beanId); return context.getBean(beanId); } public synchronized void bulidContext() { String[] contextRef = new String[1]; if ("internal".equals(clientSite)){ contextRef = new String[] { "classpath:spring-context-internal.xml" }; } else { contextRef = new String[] { "classpath:spring-context-external.xml" }; } if (context == null){ context = new ClassPathXmlApplicationContext(contextRef); } } }
package net.gbicc.x27.util.web; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import org.springframework.context.ApplicationContext; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.WebApplicationContextUtils; /** * 扩展ContextLoaderListener类 * 在WEB 系统启动时将ApplicationContext单例压人BeanFactory中 * @author BPM05 * */ public class NeqContextLoaderListener extends ContextLoaderListener { public void contextInitialized(ServletContextEvent event){ try { super.contextInitialized(event); } catch (Exception e) { e.printStackTrace(); } System.out.println("=========================NeqContextLoaderListener"); ServletContext context = event.getServletContext(); ApplicationContext ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(context); NeqBeanFactory.getInstance().setContext(ctx); } }