环境信息
| 系统 | 版本 |
|---|---|
| SpringBoot | 2.1.8.RELASE |
| Spring | 5.1.9.RELEASE |
| JDK | 1.8+ |
| Maven | 3.3+ |
配置文件
配置文件优先级
SpringBoot中默认使用application.properties和application.yml,yml格式有层次感,使用方便 其中,appcation.properties的优先级高于application.yml
配置文件默认可以放在下面4个位置
1.项目根目录下的config目录
2.项目根目录下
3.resources/config目录下
4.resources目录下
优先级从上到下,依次降低
自定义配置文件
配置文件默认存在于上述4个位置,默认名称为application,可以自定义用户目录和名称
自定义路径:
spring.config.location=classpath:/myConfig/ ;
自定义配置文件名称
spring.config.name=myConfig
属性注入
使用@Value注解
编写Student实体类com.xyz.entity.Student
package com.xyz.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;
//@Repository实现DAO层的Bean扫描
@Repository
//@PropertySource引入外部配置文件,结合@Value注解完成属性的值注入
@PropertySource("/config/student.properties")
public class Student {
@Value("${student.name}")
private String name;
@Value("${student.address}")
private String address;
@Value("${student.age}")
private int age;
@Value("${sex}")
private String sex;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
使用配置文件config/student.properties
student.name=xyz
student.address=bj
student.age=1
sex=0
编写rest访问入口com.xyz.controller.StudentController
package com.xyz.controller;
import com.xyz.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//@RestController是Spring4之后引入的新注解,等于@ResponseBody+@Controller
public class StudentController {
@Autowired
private Student student;
@RequestMapping("/student")
public String student() {
return "student:" + student;
}
}
通过 http://localhost:8083/student查看结果
使用@ConfigurationProperties注解
编写Teacher实体类com.xyz.entity.Teacher
package com.xyz.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Repository;
@Repository
@PropertySource("/config/teacher.properties")
//使用@ConfigurationProperties需要配置get和set方法,默认不需要prefix,如果变量值存在共同的前缀,可以使用prefix
//@ConfigurationProperties(prefix = "teacher")
@ConfigurationProperties
public class Teacher {
private String name;
private int age;
private String className;
private String homeAddress;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", className='" + className + '\'' +
", homeAddress='" + homeAddress + '\'' +
'}';
}
}
使用配置文件config/teacher.properties
#teacher.name=gzf
#teacher.age=100
#teacher.classname=248
#teacher.homeaddress=sxyc
name=gzf
age=100
classname=248
homeaddress=sxyc
编写rest访问接口com.xyz.controller.TeacherController
package com.xyz.controller;
import com.xyz.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TeacherController {
@Autowired
private Teacher teacher;
@RequestMapping("/teacher")
public String teacher() {
return "teacher:" + teacher;
}
}
通过http://localhost:8083/teacher查看结果
总结
SpringBoot中@Value注解和@ConfigurationProperties注解的区别
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 单个指定 |
| 松散绑定(松散语法) | 支持 | 不支持 |
| spEL | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |
参考www.cnblogs.com/slowcity/p/…
多环境配置文件
在实际应用中,需要不同的配置环境,格式为application-{profile}.properties,profile代表环境标识,如:
application-dev.properties
application-test.properties
application-prod.properties
使用时在application.properties中增加spring.profiles.active=prod
对应application-prod.properties内容为: server.port=8091