package com.byd.mes.util; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import com.byd.mes.bussiness.bo.main.ShopOrderBO; import com.byd.mes.bussiness.service.activity.Activity; import com.byd.mes.util.cache.CacheManager; public class BeanFactory implements ApplicationContextAware { private static final Log log = LogFactory.getLog(BeanFactory.class); private static ApplicationContext applicationContext; private static Map<String, String> systemParamters; private static PropertyConfigurer propertyConfigurer; public static final String INSTANCE_SINGLETON = "singleton"; public static final String INSTANCE_PROTOTYPE = "prototype"; private static Map clazzInstanceContext= new HashMap(); public static final Map<String,ShopOrderBO> map=new HashMap<String,ShopOrderBO>(); public static Object getBean(String beanName) { return applicationContext.getBean(beanName); } public static Activity getFunction(String className) { return getFunction(className,BeanFactory.INSTANCE_SINGLETON); } public static Activity getFunction(String className,String instanceType) { Object obj = getClazzIns(className,instanceType); if(!(obj instanceof Activity)) throw new RuntimeException("必须实现com.byd.mes.bussiness.service.siteoperation.sfc.Function接口"); return (Activity) obj; } public static Object getClazzIns(String className,String instanceType) { try{ if(StringUtils.equalsIgnoreCase(INSTANCE_SINGLETON,instanceType)){ Object fn = clazzInstanceContext.get(className); if(fn == null){ fn = BeanUtils.instantiateClass(Class.forName(className)); clazzInstanceContext.put(className, fn); } return fn; } else if(StringUtils.equalsIgnoreCase(INSTANCE_PROTOTYPE,instanceType)){ return BeanUtils.instantiateClass(Class.forName(className)) ; } }catch(Exception ex){ throw new RuntimeException("实例化类'"+className+"'失败 ->"+ex.getMessage(),ex); } throw new RuntimeException("实例化类'"+className+"'失败 "); } @Override public void setApplicationContext(ApplicationContext applicationContext)throws BeansException { log.info("set applicationContext to static>>>"+applicationContext); BeanFactory.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Map<String, String> getSystemParamters() { return (Map)propertyConfigurer.getProperties(); } public void setPropertyConfigurer(PropertyConfigurer propertyConfigurer) { BeanFactory.propertyConfigurer = propertyConfigurer; } }