zoukankan      html  css  js  c++  java
  • beanFactory

    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;
    	}
    
    }
    
  • 相关阅读:
    关于MAC下重置MYSQL密码
    MAC下配置PHPStorm环境
    Java中从控制台输入数据的几种常用方法
    IDEA 指定入口class
    Python中的除法
    Python 学习笔记
    SQLiteDatabase中query、insert、update、delete方法参数说明
    listview与sqlite数据绑定
    java中HashMap详解
    只要有信心,人永远不会挫败
  • 原文地址:https://www.cnblogs.com/qq1988627/p/6606894.html
Copyright © 2011-2022 走看看