springboot2读取配置文件

8 篇文章 0 订阅

关键词:springboot2、配置文件

springboot读取配置文件,无外乎就两种情况:1)读取默认的application.yml;2)读取自定义的配置文件xxx.yml。由于版本升级,springboot2和springboot1读取自定义配置文件稍微有些区别,主要体现在springboot2种@configurationProperties的注解去掉了locations参数,需与@PropertySource配合使用。

1)读取默认的application.yml

使用@Value和$读取

@Service(value = "questionService")
public class QuestionServiceImpl implements QuestionService {

    @Value("${HanLP.CustomDictionary.path.relationshipDict}")
    private String relationshipDictPath;

    @Value("${HanLP.CustomDictionary.path.personDict}")
    private String personDictPath;
}

application.yml

#HanLP 用户自定义扩展词库,不建议使用HanLP自定义词典追加的模式,建议自行加载
HanLP:
  CustomDictionary:
    path:
      relationshipDict: ${rootDirPath}/dictionary/custom/relationshipDict.txt
      personDict: ${rootDirPath}/dictionary/custom/personDict.txt

2)读取自定义的配置文件appconfig.yml

@Component
@ConfigurationProperties(prefix = "mc")
@PropertySource(value = "classpath:appconfig.yml")
public class AppConfig {

    @Value("${cinema}")
    private String cinema;

    public String getCinema() {
        return cinema;
    }

    public void setCinema(String cinema) {
        this.cinema = cinema;
    }
}

注:使用@ConfigurationProperties指定前缀、@PropertySource指定自定义的值、@Value读取属性值,本地测试去掉@Value,cinema读取出来为空,使用@EnableConfigurationProperties也不好使。

appconfig.yml

#应用配置

#电影院 贵阳万达影城中大广场店
mc:
  cinema: 1916

个人习惯使用yml的配置文件,properties同样的道理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值