目录
- 一、SpringBoot读取配置文件
- 二、@ConfigurationProperties
- 三、文件占位符随机数
- 四、SpringBoot多环境配置
- 五、SpringBoot配置端口号和上下文访问路径
一、SpringBoot读取配置文件
- 1.SpringBoot配置文件分类
- a)application.properties或者Bootstrap.properties
- b)application.yml或者Bootstrap.yml(我们更推荐使用yml格式的配置文件)
- 2.加载顺序
- bootstrap.yml 先加载 application.yml后加载
- bootstrap.yml 用于应用程序上下文的引导阶段,由父Spring ApplicationContext加载
- 3.区别
- bootstrap.yml 和 application.yml 都可以用来配置参数
- bootstrap.yml 用来程序引导时执行,应用于更加早期配置信息读取。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。一旦bootStrap.yml 被加载,则内容不会被覆盖
- application.yml 可以用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等
- 4.案例实现
- 使用
@Value
注解实现读取配置
sjyl:
name: yml
age: 22
@RestController
public class HelloWorldService {
@Value("${sjyl.name}")
private String name;
@Value("${sjyl.age}")
private int age;
@RequestMapping("/index")
public String index() {
return name + ":!!!" + age;
}
}
- 5.Properties在线转换yml
- 地址:https://www.toyaml.com/index.html
二、@ConfigurationProperties
- 1.@ConfigurationProperties作用
- 在前面的配置文件读取中,我们发现一个问题对于每一个属性都需要一个@Value来配置对应关系,这样非常冗余,一旦属性很多,并且其他类也有用到的时候,就都需要配置
- 而使用@ConfigurationProperties就可以节省@Value的配置
- 2.pom依赖
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 3.实体类UserEntity
- 类增加@Component注解
- 类增加@ConfigurationProperties注解,并指定前缀对应yml的前缀
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "sjyl")
public class UserEntity {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserEntity{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- 4.HelloWorldService
@RestController
public class HelloWorldService {
@Autowired
UserEntity userEntity;
@RequestMapping("/getConfig")
public String getConfig(){
return userEntity.toString();
}
}
三、文件占位符随机数
- 在SpringBoot的配置文件中,我们可以使用SpringBoot提供的的一些随机数
-
${random.value}、${random.int}、${random.long}、${random.int(10)}、${random.int[1024,65536]}、${app.name:默认值}
指定找不到属性时的默认值
需要注意的是:这个随机数在项目启动的时候就生成了,只有重新启动才会再随机新的值,否则不会改变
sjyl:
name: yml
age: ${random.int}
四、SpringBoot多环境配置
- 1.application.yml
- active就是我们需要指定的当前环境后缀,只需要改变active的配置就可以实现切换环境
spring:
profiles:
active: prd
- 2.application-dev.yml
## SpringBoot默认读取配置文件名称:
##application-prd.yml 生产环境配置文件
##application-dev.yml 开发环境配置文件
##application-test.yml 测试环境配置文件
sjyl:
addres: dev.sjyl.com
age: ${random.int(10)}
name: devsjyl
- 3.application-prd.yml
## SpringBoot默认读取配置文件名称:
##application-prd.yml 生产环境配置文件
##application-dev.yml 开发环境配置文件
##application-test.yml 测试环境配置文件
sjyl:
addres: prd.sjyl.com
age: ${random.int(10)}
name: prdsjyl
- 4.application-test.yml
## SpringBoot默认读取配置文件名称:
##application-prd.yml 生产环境配置文件
##application-dev.yml 开发环境配置文件
##application-test.yml 测试环境配置文件
sjyl:
addres: test.sjyl.com
age: ${random.int(10)}
name: testsjyl
五、SpringBoot配置端口号和上下文访问路径
- 1.核心配置
- 修改application.yml配置如下
- 修改后的访问地址:http://localhost:8088/sjyl/getConfig
spring:
profiles:
active: prd
server:
###端口号
port: 8088
servlet:
###设置SpringBoot项目访问路径
context-path: /sjyl