zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
    

      

  • 相关阅读:
    排查线上问题常用的几个Linux命令
    OAuth2简易实战(一)-四种模式
    程序员必备的网站推荐
    C++ sizeof
    C++ 求余运算符
    C++ mutable(可变的)
    C++ const_cast用法
    C++常变量和文字常量
    C++中 <iso646.h>头文件
    java-网络编程-使用URLDecoder和URLEncoder
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/3021679.html
Copyright © 2011-2022 走看看