美文网首页
日拱一卒:属性配置2

日拱一卒:属性配置2

作者: Tinyspot | 来源:发表于2023-09-07 21:56 被阅读0次

    1. 属性类

    @Component
    public class UserProperties implements InitializingBean {
    
        public static final String GROUP_ID = "concrete_config";
        public static final String DATA_ID = "com.concrete.config.UserProperties";
    
        // 注意:属性一定要有 Getter/Setter
        private String shopIds = null;
        private List<String> shopIdList;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            this.init();
        }
    
        private void init() throws IOException {
            this.parseConfigInfo(UserPropertiesConfig.getConfig(DATA_ID, GROUP_ID, 10 * 1000));
    
            UserPropertiesConfig.addListener(DATA_ID, GROUP_ID, new ManagerListener() {
                @Override
                public void receiveConfigInfo(String configInfo) {
                    parseConfigInfo(configInfo);
                }
    
                @Override
                public Executor getExecutor() {
                    return null;
                }
            });
        }
    
        private void parseConfigInfo(String configInfo) {
            if (StringUtils.isBlank(configInfo)) {
                return;
            }
    
            try {
                List<String> list = Splitter.on("\n").trimResults().omitEmptyStrings().splitToList(configInfo);
    
                for (String s : list) {
                    String[] kv = StringUtils.split(s, '=');
                    if (kv == null || kv.length < 1) {
                        return;
                    }
                    BeanUtils.copyProperty(this, StringUtils.trim(kv[0]), StringUtils.trim(kv[1]));
                }
    
                // 设置属性值
                this.setShopIdList(Splitter.on(",").omitEmptyStrings().trimResults().splitToList(this.getShopIds()));
            } catch (Exception e) {
                System.out.println("配置解析错误...");
            }
        }
    
        public String getShopIds() {
            return shopIds;
        }
    
        public void setShopIds(String shopIds) {
            this.shopIds = shopIds;
        }
    
        public List<String> getShopIdList() {
            return shopIdList;
        }
    
        public void setShopIdList(List<String> shopIdList) {
            this.shopIdList = shopIdList;
        }
    }
    

    2. 配置监听器

    public class UserPropertiesConfig {
    
        static public void addListener(String dataId, String group, ManagerListener listener) {
            addListeners(dataId, group, Arrays.asList(listener));
        }
    
        static public void addListeners(String dataId, String group, List<? extends ManagerListener> listeners) {
            for (ManagerListener listener : listeners) {
                // 暂时直接获取值
                String content = getConfig(dataId, group, 10 * 1000);
                listener.receiveConfigInfo(content);
            }
        }
    
        static public String getConfig(String dataId, String group, long timeout) {
            return "shopIds=111,222 \n other=00,10";
        }
    }
    
    public interface ManagerListener {
        Executor getExecutor();
    
        void receiveConfigInfo(final String configInfo);
    }
    

    3. 获取配置

    @RestController
    public class UserConfigController {
        @Resource
        private UserProperties userProperties;
    
        @RequestMapping("/user/properties")
        public String userProperties() {
            return JSON.toJSONString(userProperties.getShopIdList());
        }
    }
    

    相关文章

      网友评论

          本文标题:日拱一卒:属性配置2

          本文链接:https://www.haomeiwen.com/subject/hdervdtx.html