Spring Boot配置文件

配置获取顺序

      这里写图片描述

多环境配置

命名规范: application-{profile}.properties,profile表示不同环境,如日常、预发和线上等;
文件路径:与application.properties相同;
环境指定:在application.properties文件中进行设定:spring.profiles.active={profile};
使用说明:

  1. application.properties中配置通用内容(与环境无关的);
  2. application-{profile}.properties中配置与环境相关的内容;

这里写图片描述

配置间引用

方式:${key}
示例:

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

默认配置文件application.properties

文件路径: classpath或者classpath的/config目录下;
文件内容:全局通用配置;

获取配置

方式:通过Environment获取;
示例:

ConfigurableApplicationContext ctx = SpringApplication.run(Main.class, args);
System.out.println(ctx.getEnvironment().getProperty("env"));

配置注入Bean

方式一:@ConfigurationProperties,示例查看
方式二:@Value(“${property}”);

@ConfigurationProperties vs. @Value

这里写图片描述

参考:

  1. http://blog.didispace.com/springbootproperties/
  2. 官网:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-external-config

猜你喜欢

转载自blog.csdn.net/yangguosb/article/details/80917368