通过代码读取配置文件内容

// 可以使用静态代码块和属性,也可以使用非静态代码块和属性
    public static String active;

    static {
        // 读取yml文件
        ClassPathResource resource = new ClassPathResource("application.yml");
        try {
            // 读取properties文件
//            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            yaml.setResources(resource);
            Properties properties = yaml.getObject();
            active = properties.getProperty("spring.profiles.active", "prod");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

 

posted on 2020-08-25 11:25  永不宕机  阅读(521)  评论(0编辑  收藏  举报

导航