java+yml+工具类_SpringBoot工具类获取yml配置文件内容

package com.common.test.utils;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;

import org.springframework.core.io.ClassPathResource;

import org.springframework.core.io.Resource;

import java.util.Properties;

/**

* Created by admin on 2019/1/4.

*/

public class PropertiesUtils {

private static String PROPERTY_NAME = "application-common.yml";

public static Object getCommonYml(Object key){

Resource resource = new ClassPathResource(PROPERTY_NAME);

Properties properties = null;

try {

YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();

yamlFactory.setResources(resource);

properties = yamlFactory.getObject();

} catch (Exception e) {

e.printStackTrace();

return null;

}

return properties.get(key);

}

public static void main(String[] args) {

System.out.println(getCommonYml("file.upload.path"));

}

}

b8a3bea48319317aa96d94d43c1098cb.png

application.yml配置信息:

spring:

profiles:

include: jdbc,common

特别注意要将application-common.yml 加入到classpath 中去,不然可能读取不到文件,这边例子只是使用的classPath中加载配置,各位也可以从文件系统获取,具体还得看各自需要了,建议使用classPath加载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值