为了保证生成的实例不相同的话,可以做如下改造

 private static Map beanmap=new HashMap();
       //为getbean方法获取bean准备一个容器,使用map容器,因为有key
    static{
       bundle=ResourceBundle.getBundle("bean");
           Enumeration keys = bundle.getKeys();
           while(keys.hasMoreElements()){
               String beanName=keys.nextElement();
               try {
                   Object o = Class.forName(bundle.getString(beanName)).newInstance();
                   beanmap.put(beanName,o);
               } catch (InstantiationException e) {
                   e.printStackTrace();
               } catch (IllegalAccessException e) {
                   e.printStackTrace();
               } catch (ClassNotFoundException e) {
                   e.printStackTrace();
               }
           }
       }

    public static  Object getbean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//        Object o = Class.forName(properties.get(beanName).toString()).newInstance();
        //Object o = Class.forName(bundle.getString(beanName)).newInstance();
        Object o = beanmap.get(beanName);
        return o;

你可能感兴趣的:(为了保证生成的实例不相同的话,可以做如下改造)