• spring, ApplicationContext,读取bean、属性、属性值


    ApplicationContext,读取bean、属性、属性值,demo:

    public class HutuAbstractTopicEventcode {
    
        private String              ctxPath    = "C:/happyday/demo.xml";
    
        private String              moduleName = "happybaby";
    
        public static void main(String[] args) {
            HutuAbstractTopicEventcode o = new HutuAbstractTopicEventcode();
            
            // 提取出所有topic/eventcode
            Map<String, Set<String>> topicEventcodes = o.abstractTopicEventcode();
            
            // 用于计数
            int count = 0;
            
            // 打印. 
            for(Map.Entry<String, Set<String>> entry : topicEventcodes.entrySet()){
                String topic = entry.getKey();
                Set<String> eventcodes = entry.getValue();
                for(String eventcode : eventcodes){
                    System.err.println(topic + ", " + eventcode);
                    count ++;
                }
            }
            System.err.println("共计: " + count);
    
        }
    
        /**
         * 从xml里提取出所有topic/eventcode,并转化为map类型.
         * 
         * @return
         */
        @SuppressWarnings("unchecked")
        public Map<String/*topic*/, Set<String/*eventcode*/>> abstractTopicEventcode() {
            ApplicationContext context = new FileSystemXmlApplicationContext(ctxPath);
            //   ApplicationContext context = new ClassPathXmlApplicationContext(ctxPath);
    
            TopicMessageConfig newConfigObject = (TopicMessageConfig) context.getBean(moduleName);
            Set<String> attributeSet = this.getAttributeNames(newConfigObject);
            for (String attributeName : attributeSet) {
                System.err.println("属性名称" + attributeName);
                System.err.println("属性值:" + this.getAttributeVlaue(newConfigObject, attributeName));
                return (Map<String, Set<String>>) this
                    .getAttributeVlaue(newConfigObject, attributeName);
            }
            return null;
        }
        
        // ~~~ 内部方法 ~~~
    
        /**
         * 获取该bean里的全部属性名称.
         * 
         * @param newConfigObject
         * @return
         */
        private Set<String> getAttributeNames(TopicMessageConfig newConfigObject) {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getBeanInfo(newConfigObject)
                .getPropertyDescriptors();
            Set<String> attributeNames = new HashSet<String>();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (!propertyDescriptor.getName().equals("class")) {
                    if (propertyDescriptor.getReadMethod() != null
                        && propertyDescriptor.getWriteMethod() != null) {
                        attributeNames.add(propertyDescriptor.getName());
                    }
                }
            }
            return attributeNames;
        }
    
        /**
         * 获取该bean中,对应该属性名的属性值.
         * 
         * @param configObject
         * @param attributeName
         * @return
         */
        private Object getAttributeVlaue(Object configObject, String attributeName) {
            PropertyDescriptor[] propertyDescriptors = BeanUtils.getBeanInfo(configObject)
                .getPropertyDescriptors();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (propertyDescriptor.getName().equals(attributeName)) {
                    try {
                        if (propertyDescriptor.getReadMethod() != null) {
                            return propertyDescriptor.getReadMethod().invoke(configObject);
                        }
                    } catch (InvocationTargetException e) {
                        this.handleException("get", attributeName, e);
                    } catch (IllegalAccessException e) {
                        this.handleException("get", attributeName, e);
                    }
    
                }
            }
            return null;
        }
    
        /**
         * 处理异常.
         * 
         * @param opName
         * @param name
         * @param e
         */
        private void handleException(String opName, String name, Exception e) {
            StringBuilder buf = new StringBuilder();
            buf.append(opName).append("属性失败,moduleName:").append(this.moduleName);
            buf.append(", propertyName:").append(name);
            LOGGER.error(buf.toString(), e);
            throw new RuntimeException(buf.toString(), e);
        }
    
    }
    

      

  • 相关阅读:
    《需求工程——软件建模与分析》阅读笔记03
    第十一周周五
    统计字符串里每个词语的数目
    第十一周周四计划
    for循环创建的a标签,当点击时如何确定点击的是哪一个标签?
    第十周计划周二&周三计划
    第十周周二汇报
    第九周周五小思路
    第九周周五计划&&周四总结
    第九周周四计划&&周三总结
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/3021679.html
Copyright © 2020-2023  润新知