Spring-Boot导入配置文件与取值

前言:

springboot简化了大量配置文件,但是必要时还是需要导入配置文件的,比如dubbo,此处简记之。


正文:

所有的配置文件引入都是使用注解在类上进行引入的,常用的有两种注解@PropertySource和@ImportSource,分别导入properties文件和xml文件


@PropertySource注解

引入单个properties文件:

@PropertySource(value = {"classpath : xxxx/xxx.properties"})

引入多个properties文件:

@PropertySource(value = {"classpath : xxxx/xxx.properties","classpath : xxxx.properties"})


@ImportSource注解 :可以额外分为两种模式 相对路径classpath,绝对路径(真实路径)file

注意:单文件可以不写value或locations,value和locations都可用

相对路径(classpath):

引入单个xml配置文件:

@ImportSource("classpath : xxx/xxxx.xml")

引入多个xml配置文件:

@ImportSource(locations={"classpath : xxxx.xml" , "classpath : yyyy.xml"})

绝对路径(file):

引入单个xml配置文件:

@ImportSource(locations= {"file : d:/hellxz/dubbo.xml"})

引入多个xml配置文件:

@ImportSource(locations= {"file : d:/hellxz/application.xml" , "file : d:/hellxz/dubbo.xml"})


取值:

使用@Value注解取配置文件中的值

@Value("${properties中的键}")

private String xxx;

 

尾声:

如本文中有错误,还望评论告知一二,欢迎拍砖

原文地址:https://www.cnblogs.com/hellxz/p/8763596.html