一、外部配置及优先级

SpringBoot的外部配置属性值官方给出了很多种方式,以便可以在不同的环境中使用相同的代码。

其使用了非常特别的PropertySource命令,旨在允许合理的覆盖值。当然,如果属性值不同,则这些配置方式中的属性值都会被加载;按照从高到低的排序如下:

(1)、在您的HOME目录设置的Devtools全局属性(~/.spring-boot-devtools.properties)。

(2)、单元测试中的 @TestPropertySource 注解。

(3)、单元测试中的 @SpringBootTest#properties 注解属性

(4)、命令行参数。

SPRING_APPLICATION_JSON=‘{"foo":{"bar":"spam"}}‘ java -jar myapp.jar

(6)、ServletConfig 初始化参数。

(7)、ServletContext 初始化参数。

(8)、来自 java:comp/env 的JNDI属性。

(9)、Java系统属性(System.getProperties())。

(10)、操作系统环境变量。

(11)、RandomValuePropertySource,只有随机的属性 random.* 中。

(12)、jar包外面的 Profile-specific application properties (application- {profile} .properties和YAML)

(13)、jar包内的 Profile-specific application properties (application-{profile}.properties和YAML)

(14)、jar包外的应用属性文件(application.properties和YAML)。

(15)、jar包内的应用属性文件(application.properties和YAML)。

(16)、在@Configuration上的@PropertySource注解。

(17)、默认属性(使用SpringApplication.setDefaultProperties设置)。

在具体的讲解这些配置的时候我们先来做一些准备工作,

(1)、书写一个Controller

@RestController
@Slf4j
public class MyController {@Value("${name}")private String name;@GetMapping("/getDefaultProperties")public String getDefaultProperties() {return name;}

1、使用SpringApplication.setDefaultProperties设置默认属性

在应用程序主类main方法中在调用run方法之前,设置默认值

@SpringBootApplication
public class Application {public static void main(String[] args) {//SpringApplication.run(Application.class, args);Properties properties = new Properties();properties.setProperty("name", "(17)、默认属性(使用SpringApplication.setDefaultProperties设置)");SpringApplication application = new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);//new SpringApplicationBuilder()// .sources(Application.class)// .bannerMode(Banner.Mode.OFF)// .properties(properties)// .run(args);}
}

此时你访问http://localhost:8080/getDefaultProperties在页面上输出的内容为

备注:如果你想把你项目中的所有的配置放到配置中心Apollo的话,这种方式也是可以很方法的实现的。2、在@Configuration上的@PropertySource注解

@SpringBootApplication
@PropertySource(value = {"classpath:test/propertySource.properties"}, encoding = "UTF-8")
public class Application {public static void main(String[] args) {Properties properties = new Properties();properties.setProperty("name", "(17)、默认属性(使用SpringApplication.setDefaultProperties设置)");SpringApplication application = new SpringApplication(Application.class);application.setDefaultProperties(properties);application.run(args);}
}

3、jar包内的应用属性文件(即默认的配置文件)

name=(15)、jar包内的应用属性文件

4、jar包外的应用属性文件

(1)、将你的应用程序打包成可运行的jar,在jar包的同级目录中放置一个application.properties文件,里面的内容如下:

name=(14)、jar包外的应用属性文件

5、jar包内的 Profile-specific application properties

新建application-test.properties文件,里面的内容如下

name=(13)、jar包内的 Profile-specific application properties

设置启动参数

访问链接得到下图结果

6、jar包外面的 Profile-specific application properties

(1)、将你的应用程序打包成可运行的jar,在jar包的同级目录中放置一个application-test.properties文件,里面的内容如下:

name=(14)、jar包外的应用属性文件

(2)、java -jar -Dspring.profiles.active=test ***.jar

springboot读取外部和内部配置文件的方法,如下优先级:

第一种是在执行命令的目录下建config文件夹。(在jar包的同一目录下建config文件夹,执行命令需要在jar包目录下才行),然后把配置文件放到这个文件夹下。

第二种是直接把配置文件放到jar包的同级目录。

第三种在classpath下建一个config文件夹,然后把配置文件放进去。

第四种是在classpath下直接放配置文件。

springboot默认是优先读取它本身同级目录下的一个config/application.properties 文件的。

在src/main/resources 文件夹下创建的application.properties 文件的优先级是最低的

7、命令行属性

默认情况下,SpringApplication将任何命令行选项参数(以'-- '开头,例如--server.port=9000)转换为属性,并将其添加到Spring环境中。 如上所述,命令行属性始终优先于其他属性来源。

如果不希望将命令行属性添加到环境中,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它们。

二、properties文件中的占位符

application.properties中的值在使用时通过已有的环境进行过滤,以便可以引用之前已经定义好的值

app.name=MyApp
app.description=${app.name} is a Spring Boot application

三、使用YAML替代 Properties

YAML是JSON的超集,因此这是分层配置数据一种非常方便的格式,。 每当您的类路径中都有SnakeYAML库时,SpringApplication类将自动支持YAML作为 properties 的替代方法。

如果您使用“Starters”,SnakeYAML将通过spring-boot-starter自动提供。

1、加载yaml

Spring Framework提供了两个方便的类,可用于加载YAML文档。 YamlPropertiesFactoryBean将YAML作为Properties加载,YamlMapFactoryBean将YAML作为Map加载。

例如下面这个YAML文档

environments:dev:url: http://dev.bar.comname: Developer Setupprod:url: http://foo.bar.comname: My Cool App

将转换为属性

environments.dev.url=http://dev.bar.com
environments.dev.name=Developer Setup
environments.prod.url=http://foo.bar.com
environments.prod.name=My Cool App

YAML列表表示为具有[index] dereferencers的属性键,例如YAML:

my:servers:- dev.bar.com- foo.bar.com

将转化为属性:

my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

2、通过@ConfigurationProperties将配置绑定到Bean

(1)、配置bean的书写

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "my")
public class MyServerProperties {private String name;private List<String> servers = new ArrayList<>();
}

(2)、application.yaml文件书写

my:servers:- dev.bar.com- foo.bar.comname: myName

3、YAML的缺点

YAML文件无法通过@PropertySource注解加载。 因此,在需要以这种方式加载值的情况下,需要使用properties文件。

四、类型安全的配置属性

使用@Value(“${property}”)注释来注入配置属性有时可能很麻烦(类型常规配置),特别是如果您正在使用多个层次结构的属性或数据时。 Spring Boot提供了一种处理属性的替代方法,允许强类型Bean管理并验证应用程序的配置。

(1)、Bean的定义

/*** 类型安全的配置属性** @Author YUBIN* @create 2019-06-16*/
@ConfigurationProperties("foo")
public class FooProperties {private boolean enabled;private InetAddress remoteAddress;private final Security security = new Security();public boolean isEnabled() {return enabled;}public void setEnabled(boolean enabled) {this.enabled = enabled;}public InetAddress getRemoteAddress() {return remoteAddress;}public void setRemoteAddress(InetAddress remoteAddress) {this.remoteAddress = remoteAddress;}public Security getSecurity() {return security;}public static class Security {private String username;private String password;private List<String> roles = new ArrayList<>(Collections.singleton("USER"));public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public List<String> getRoles() {return roles;}public void setRoles(List<String> roles) {this.roles = roles;}}
}

(2)、YAML文件中的配置

foo:remote-address: 192.168.1.1security:username: fooroles:- USER- ADMIN

(3)、应用程序主类上加上必要的注解

@EnableConfigurationProperties({FooProperties.class})
public class Application {

这样在程序中就可以使用@Autowired的形式注入配置类了

@RestController
@Slf4j
public class MyController {@Autowiredprivate FooProperties fooProperties;@GetMapping("/getFooProperties")public String getFooProperties() {return JSON.toJSONString(fooProperties);}

五、第三方配置

现在我们在构建一个名为"yubin-common",类型为jar的maven项目

1、书写一个配置类

@ConfigurationProperties(prefix = "bar")
public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}

在之前的项目中引入此项目的依赖;这时如果在之前的项目中需要使用BarComponent这个属性类的话,则需要通过@EnableConfigurationProperties(BarComponent.class)这个注解来引入类,但是这样做是否合理呢?一个项目这么做,当有其它的项目也需要这个类的话是不是也要这么做呢?

public class BarComponent {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}
@Configuration
public class ConfigurationBean {@Bean@ConfigurationProperties(prefix = "bar")public BarComponent barComponent() {return new BarComponent();}
}

2、小结

@ConfigurationProperties导入外部属性填充到这个Bean的实例,有三种方式:

(1)、@ConfigurationProperties + @Component 注解到bean定义类上

(2)、@ConfigurationProperties + @Bean注解在配置类的bean定义方法上

(3)、@ConfigurationProperties注解到普通类然后通过@EnableConfigurationProperties定义为bean

六、宽松的绑定

Spring Boot使用一些宽松的规则将环境属性绑定到@ConfigurationProperties bean,因此不需要在Environment属性名称和bean属性名称之间进行完全匹配。 常用的例子是这样有用的:虚分离(例如上下文路径绑定到contextPath)和大写(例如PORT绑定到端口)环境属性。

配置bean

/*** 属性bean宽松绑定演示** @Author YUBIN* @create 2019-06-16*/
@Component
@ConfigurationProperties(prefix = "person")
@Getter
@Setter
public class OwnerProperties {private String firstName; //person.firstName 标准骆峰命名法。private String secondName; // person.second-name 虚线符号,推荐用于.properties和.yml文件。private String thirdName; // person.third_name 下划线符号,用于.properties和.yml文件的替代格式。private String fourName; // PERSON_FOUR_NAME 大写格式 推荐使用系统环境变量时。
}

测试类

@RestController
@Slf4j
public class MyController {@Autowiredprivate OwnerProperties ownerProperties;@GetMapping("/getOwnerProperties")public String getOwnerProperties() {return JSON.toJSONString(ownerProperties);}
}

七、@ConfigurationProperties验证

如果你需要验证@ConfigurationProperties类。 您可以直接在配置类上使用JSR-303 javax.validation约束注解 @Validated。 只需确保您的类路径中符合JSR-303实现,然后在您的字段中添加约束注释:

@ConfigurationProperties("foo")
@Validated
public class FooProperties {private boolean enabled;@NotNullprivate InetAddress remoteAddress;
// ... getters and setters

为了验证嵌套属性的值,您必须将关联字段注释为@Valid以触发其验证。 例如,基于上述FooProperties示例:

@ConfigurationProperties(prefix="connection")
@Validated
public class FooProperties {@NotNullprivate InetAddress remoteAddress;@Validprivate final Security security = new Security();// ... getters and setterspublic static class Security {@NotEmptypublic String username;// ... getters and setters}
}

@ConfigurationProperties 对比 @Value

@Value是核心容器功能,它不提供与类型安全配置属性相同的功能。 下表总结了@ConfigurationProperties和@Value支持的功能:

八、配置文件Profiles

1、代码层面区分环境

Spring 配置文件提供了将应用程序配置隔离的方法,使其仅在某些环境中可用。 任何@Component或@Configuration都可以使用@Profile进行标记,以限制其在什么时候加载:

(1)、测试环境

@Service
@Profile({"test"})
public class TestProfileServiceImpl implements ProfileService {@Overridepublic String getEnvironment() {return "test环境";}
}

(2)、dev环境

@Service
@Profile({"dev"})
public class DevProfileServiceImpl implements ProfileService {@Value("${spring.profiles.active}")private String profileActive;@Overridepublic String getEnvironment() {return "dev环境";}
}

(3)、测试类

@RestController
@Slf4j
public class MyController {@Autowiredprivate ProfileService profileService;@GetMapping("/getProfileService")public String getProfileService() {return profileService.getEnvironment();}
}

当设置启动参数为 spring.profiles.active=dev时,页面展示效果:

image.png

当设置启动参数为 spring.profiles.active=test时,页面展示效果:

读取外部配置文件_SpringBoot外部配置、优先级及配置详解相关推荐

  1. Spring零配置之@Configuration注解详解

    转载自 Spring零配置之@Configuration注解详解 @Configuration介绍 Spring3.0之前要使用Spring必须要有一个xml配置文件,这也是Spring的核心文件,而 ...

  2. eclipse配置python开发环境_Eclipse中配置python开发环境详解

    Eclipse中配置python开发环境详解 1.下载python安装包.python-2.6.6.msi.并安装. 默认python会安装在C:\Python26下,查看环境变量,如果没有在path ...

  3. replica文件服务器,MongoDB_mongodb replica set 配置高性能多服务器详解,mongodb的多服务器配置,以前写 - phpStudy...

    mongodb replica set 配置高性能多服务器详解 mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-s ...

  4. sendmail mysql_CentOS配置sendmail服务器命令详解

    CentOS配置sendmail服务器命令详解 [root@localhost ~]# rpm -q sendmail package sendmail is not installed [root@ ...

  5. python怎么读文件内容-Python读取文件内容为字符串的方法(多种方法详解)

    以下笔记是我在 xue.cn 学习群之数据分析小组所整理分享的心得.相关背景是:我选择中文词频统计案例作为考察大家python基础功掌握程度. 以小见大,下面是2个小技能的具体实战: 如何灵活地处理文 ...

  6. Win7系统Visual Studio 2013配置OpenCV3.1图文详解

    Win7系统Visual Studio 2013配置OpenCV3.1图文详解 OpenCV3.1对硬件加速和移动开发的支持相对于老版本都有了较大改进,支持新的开发工具,更易于扩展,配置方式也比以前简 ...

  7. pycharm导入python环境是空的_PyCharm导入python项目并配置虚拟环境的教程详解

    PyCharm导入python项目并配置虚拟环境的教程详解 进入PyCharm后,点击File→Open,然后在弹窗中选择需要导入项目的文件夹: 打开了python项目后,需要配置该项目对应的pyth ...

  8. java spring mvc 上传_Java Spring MVC 上传下载文件配置及controller方法详解

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) 下载文件代码 @RequestMapping("/file/{name.rp}") public Respo ...

  9. 如何配置Mybatis?(详解)

    如何配置Mybatis?(详解) 官网文档: https://mybatis.org/mybatis-3/zh/getting-started.html pom.xml <?xml versio ...

最新文章

  1. ssh远程操作服务器
  2. Django的静态文件的配置
  3. 安装mavlink遇到的问题(future找不到)
  4. 判断true的正确做法
  5. webrtc在远程助手应用的实践
  6. 深度学习主机攒机小记
  7. 对自定义UITableViewCell的理解
  8. Open3d之坐标变换
  9. 如何看到格式化的json文件
  10. Graylog日志管理系统---搜索查询方法使用简介
  11. (转)Python之区块链入门
  12. MATLAB变压器设备故障模型,电力变压器内部故障简便仿真模型
  13. oracle赋权directory,ORACLE DIRECTORY目录管理步骤
  14. Excel 2010 VBA 入门 010 VBE编辑器的工具栏
  15. mysql 小于号转义_mybatis sql语句配置大于号小于号的处理
  16. PPT修行之路(一)
  17. 华为消费者业务公布2017上半年智能手机收入暴涨
  18. 二进制补码是如何把减法转变为加法的
  19. 【深度学习】深度学习概念的理解(一)
  20. 1089 烽火传递(单调队列优化)

热门文章

  1. 交换机实现虚拟局域网
  2. 二、【List、Set、数据结构、Collections】
  3. html坐标绘制路径,canvas学习笔记之绘制简单路径
  4. 解决pycharm运行Flask指定ip、端口更改无效
  5. 大数据集群搭建之hadoop、tomcat、jdk等工具的安装(三)
  6. 图像影音型计算机主板选择什么,电脑主板型号在哪里看? 每日一答
  7. python redis 性能测试台_Redis性能测试
  8. mysql接口可以重载吗_php 到底可不可以重载
  9. 内核中架构相关代码简介
  10. c语言建立一个链表,每个结点包括姓名和成绩,求C语言几道题的答案~~拜托了~~...