Spring的ApplicationContext.getBean()的具体实现

public class SpringUtil { private static Log logger = LogFactory.getLog(SpringUtil.class); /** Spring框架应用上下文对象 */ private static ApplicationContext factory = getApplicationContext(); static{ getApplicationContext(); } public static void setFactoryBean(ApplicationContext factory){ SpringUtil.factory = factory; } /** * 获得Spring框架应用上下文对象 * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { //判断如果 ApplicationContext 的对象 == NULL if ( factory == null ) { if(logger.isDebugEnabled()) logger.debug("===================================Init Spring's ApplicationContext========================================="); try { ConsolePrinter.println("******init spring application context"); factory = new ClassPathXmlApplicationContext(new String[]{"applicationContext-faengine.xml","applicationContext-common.xml" //,"applicationContext-daoSupport-test.xml" }); } catch ( Exception e1 ) { if(logger.isDebugEnabled()) logger.debug("err = " + e1.getMessage()); e1.printStackTrace(); } } //返回ApplicationContext return factory; }

 

调用的时候,直接使用

       ApplicationContext ac = SpringUtil.getApplicationContext();
       IModelMappingUCC modelMappingUCC = (IModelMappingUCC)ac.getBean("modelMappingUCC");

 

就可以直接使用这个对象了!

 

你可能感兴趣的:(spring,exception,框架,String,application,Class)