css與html

/**
 * @Author: Promsing
 * @Date: 2021/3/19 - 11:50
 * @Description: 模仿IOC容器,將建立的對象放在Map集合中,加載該類時會讀取配置文件中,將類放入Map中
 * @version: 1.0
 */
public class BeanFactory {
    //定義一個properties對象
    private static Properties props;
    //定義一個Map,用於存放咱們建立的對象(單例,當類加載以後就有了對象,以後從Map中獲取)
    private static Map<String,Object> beans;

    //容器
    static {
        try {
            props=new Properties();
            //將bean.properties放在了resources路徑下
            InputStream is=BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(is);
            //實例化容器
            beans=new HashMap<String,Object>();
            //從配置文件中獲取全部key值
            Enumeration<Object> keys = props.keys();
            while (keys.hasMoreElements()){
                //取出每個key
                String key = keys.nextElement().toString();
                //根據key獲取value
                String path = props.getProperty(key);
                Object value=Class.forName(path).newInstance();
                //放入容器中
                beans.put(key,value);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //提供一個訪問Map容器的入口
    public static Object  getInstance(String name){
        return beans.get(name);
    }

}
相關文章
相關標籤/搜索