spring

获取spring中所需信息

@Test
    public void setUp(ApplicationContext applicationContext) throws Exception {
        Environment environment = applicationContext.getEnvironment();
        StandardEnvironment standardEnvironment= (StandardEnvironment) environment;
        Map<String, Map<String, String>> map = new HashMap<>(8);
        MutablePropertySources propertySources = standardEnvironment.getPropertySources();
        PropertySource<?> propertySource = propertySources.get("applicationConfig: [classpath:/application.yml]");
        Object source1 = propertySource.getSource();
        Map<String,String>map1= (Map<String, String>) source1;
        log.info(String.valueOf(map1.get("butler.config.name"))+"=========butler.config.name");


        //PropertySource<?> applicationConfig =     // .get("applicationConfig");
        //Object property = applicationConfig.getProperty("bulter.config.name");
    //    log.info(property+"===========");
        Iterator<PropertySource<?>> iterator = standardEnvironment.getPropertySources().iterator();
        while (iterator.hasNext()) {
            PropertySource<?> source = iterator.next();
            Map<String, String> m = new HashMap<>(128);
            String name = source.getName();
            Object o = source.getSource();
            if (o instanceof Map) {
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) o).entrySet()) {
                    String key = entry.getKey();
                    m.put(key, standardEnvironment.getProperty(key));
                }
            }
            map.put(name, m);
        }
        log.info("{}",map);
//        String[] defaultProfiles = environment.getDefaultProfiles();
//        log.info("defaultProfiles:{}",defaultProfiles);
//        log.info("value获取到的值是:{}",appName);
    }

修改spring环境中的信息


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值