SpringBoot配置文件与生产环境和开发环境配置
周sir |
2017年6月16日 |
springboot |
0 条评论 | 5831
Spring Boot 默认读 application.yml / application.properties。开发库和生产库、端口往往不同,用 spring.profiles.active 切换 application-dev.yml 和 application-product.yml 即可,不必改代码。
配置读出来后,常用两种注入:@Value 逐项取值,或 @ConfigurationProperties 按前缀绑到一个 JavaBean。后者更适合一组相关属性。
一、生产环境与开发环境
1. application-product.yml
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/productDb
username: root
password: 123456
2. application-dev.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/devlepmentDb
username: root
password: kf123
3. application.yml 里激活当前环境
spring:
profiles:
active: dev
| 文件 |
端口 |
库 |
application-dev.yml |
8080 |
devlepmentDb |
application-product.yml |
8081 |
productDb |
spring.profiles.active |
写在主配置里 |
改这一处即可切换 |
二、用 @Value 读取
先在 application.yml 准备数据:
test:
username: viktor
password: 123
缺点是每个字段都要写一遍注解:
@Controller
@RequestMapping("/config")
public class ConfigeController {
@Value(value = "${test.username}")
private String userName;
@Value(value = "${test.password}")
private String passWord;
@RequestMapping("")
@ResponseBody
public String config() {
return "config == " + userName + " " + passWord;
}
}
三、用 @ConfigurationProperties 读取
-
类上加
@ConfigurationProperties(prefix="test"),前缀对应 test.username。
-
类属性名与配置项同名(
username / password)。
-
-
加上
spring-boot-configuration-processor 依赖,IDE 才能提示 yml 项。
<!-- 支持 @ConfigurationProperties 注解 yml 配置文件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
ConfigeControllerNew.java
@Controller
@RequestMapping("/confignew")
@ConfigurationProperties(prefix = "test")
public class ConfigeControllerNew {
private String username;
private String password;
@RequestMapping("")
@ResponseBody
public String config() {
return "config == " + username + " " + password;
}
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;
}
}
四、实际应用:抽一个属性 Bean
1. application.yml
原文 YAML 少了冒号,正确写法是:
user:
username: 张三
age: 23
2. 属性工具类
@Component
@ConfigurationProperties(prefix = "user")
public class UserProperties {
private String username;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3. 在 Controller 中注入
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private UserProperties userProperties;
@GetMapping(value = "/proper")
public String say() {
return userProperties.getUsername();
}
}
一句话总结:多环境用 profiles 拆 yml;单个值用 @Value,一组值用 @ConfigurationProperties 绑到 Bean,再在业务里注入这个 Bean。
转载请注明来源:SpringBoot配置文件与生产环境和开发环境配置
我是周sir,这是我的博客。致力于分享我学会的技术和优秀的文章。微信公众号 “周sir专栏” 欢迎大家关注。