重写PropertySourcesPlaceholderConfigurer 实现自定义配置读取

重写 PropertySourcesPlaceholderConfigurer的postProcessBeanFactory方法 实现 对配置文件中 ${}配置的自定义解析

默认情况下 ${} 是读取本地配置文件的已存在配置 当不存在时 配置为NULL 

其中主要的代码是postProcessBeanFactory方法中的 PropertySource 抽象类

if (this.environment != null) {
                this.propertySources.addLast(new PropertySource<Environment>("environmentProperties", this.environment) {
                    @Nullable
                    public String getProperty(String key) {
                        return ((Environment)this.source).getProperty(key);
                    }
                });
            }

只需要将getProperty的结果单独拿出来进行验证 并 为NULL 的情况下调用自定义的方法获取值 在return出去

            if (this.environment != null) {
                this.propertySources.addLast(new PropertySource<Environment>("environmentProperties", this.environment) {
                    @Nullable
                    public String getProperty(String key) {
                        String value = ((Environment)this.source).getProperty(key);
                        //不存在 说明非配置文件本地 配置
                        if (value == null){
                            try{
                                //存在 异步注入延迟 需要手动获取
                                if (client == null){
                                    client = beanFactory.getBean(AmdClient.class);
                                }
                                value = String.valueOf(client.getValue(key));
                            }catch (Exception e){
                                logger.error(e.toString());
                            }
                        }
                        return value;
                    }
                });
            }

就能通过自定义的方法进行动态配置

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果不重写Comparator的compare方法或使用自定义的Comparator,TreeSet会默认使用元素的自然顺序或比较器的自然顺序(如果提供了比较器),来进行元素的排序和去重。对于自定义的对象,如果没有实现Comparable接口或提供Comparator,会抛出ClassCastException。 对于题目中的需求,可以考虑使用Map来实现。我们可以使用HashMap来存储数据,并以key作为键,value和时间戳组成的对象作为值。对于重复的key,我们只需要比较时间戳,保留最新的数据。最后,我们可以使用TreeMap来对HashMap中的数据按照key自然排序,从而得到最终的结果。 下面是一个示例代码: ```java import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class Main { public static void main(String[] args) { Map<String, ValueWithTimestamp> map = new HashMap<>(); map.put("key1", new ValueWithTimestamp("value1", System.currentTimeMillis())); map.put("key2", new ValueWithTimestamp("value2", System.currentTimeMillis())); map.put("key1", new ValueWithTimestamp("value3", System.currentTimeMillis())); map.put("key2", new ValueWithTimestamp("value4", System.currentTimeMillis())); map.put("key3", new ValueWithTimestamp("value5", System.currentTimeMillis())); map.put("key3", new ValueWithTimestamp("value6", System.currentTimeMillis())); TreeMap<String, ValueWithTimestamp> treeMap = new TreeMap<>(map); for (Map.Entry<String, ValueWithTimestamp> entry : treeMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue().value); } } static class ValueWithTimestamp { String value; long timestamp; public ValueWithTimestamp(String value, long timestamp) { this.value = value; this.timestamp = timestamp; } } } ``` 输出结果为: ``` key1: value3 key2: value4 key3: value6 ``` 可以看到,重复的key被去重了,并且保留了最新的value。最后,我们使用TreeMap对数据按照key自然排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值