Spring Boot(五) 之使用配置属性
使用配置属性
1、简单理解Spring的环境抽象
在Spring中有两种不同的配置:bean装配和属性注入
直接上图
Spring环境从各个属性源拉取属性,并让Spring应用上下文中的bean可以使用它们。
2、创建自己的配置属性
在开发的过程中有时候使用自己的配置属性会让事情变得简单。暂时没有想到什么很好的例子,就简单讲讲吧。
第一种直接使用
使用configNum这个新属性,并在类上加上@ConfigurationProperties注解
@Controller
@RequestMapping("/config")
@ConfigurationProperties(prefix = "config") //第一种直接使用
public class ConfigTestController {
private int configNum = 20;
public void setConfigNum(int configNum){
this.configNum = configNum;
}
@GetMapping
@ResponseBody
public String configTest(){
return "configNum ="+this.configNum;
}
}
@ConfigurationProperties注解的prefix属性设置成"config",这就意味着设置configNum时,要使用名为config.configNum的配置属性。
configNum的默认值是20,我们也可以在配置文件中来设置config.configNum属性值。以application.yml文件为例
config:
configNum: 100
配置前(默认值)
—————————————————————————
配置后
当有比较多的配置属性时而且多个类使用时,直接使用就比较的麻烦。所以我们可以用第二种定义配置属性的持有者 ,也就是说将这些配置属性放到特定的bean中。
- 将配置放到一个特定的类中,configNum和configStr两个配置属性
需要在类上加上@ConfigurationProperties注解
@Component
@ConfigurationProperties(prefix = "bean.config")
@Data
public class ConfigBean {
private int configNum = 20;
private String configStr = "This is default String";
}
- 注入到任意需要这些属性的其他bean中
@Controller
@RequestMapping("/config")
public class ConfigTestController {
/**
* 下面演示第二种:定义配置属性持有者
* 1、将配置放在一个特定的类中(ConfigBean)
* 2、注入需要更改配置的类中
*/
private ConfigBean configBean;
@Autowired
public ConfigTestController(ConfigBean configBean){
this.configBean = configBean;
}
@GetMapping
@ResponseBody
public String configTest(){
return "configNum = "+configBean.getConfigNum()+",configStr ="+configBean.getConfigStr();
}
}
配置前
————————————————————————
在application.yml文件配置后(configNum等价于config-num)同样configStr等价于config-str。
bean:
config:
config-num: 100
config-str: This is the modified string
配置后:
—————————————————————————
3、使用profile进行配置
当应用部署到不同环境,一些配置也有些不同。例如在开发环境下,使用嵌入式的H2数据库更方便,日志级别为DEBUG。但是在生产环境中,一般就要用外部的数据库MySQL,日志级别设为WARN。在开发环境设置开发时候的配置,部署到生产环境又要改成适合生产环境的配置,比较麻烦。
3.1定义特定的profile的属性
- 第一种方式:创建另外一个YAML文件或属性文件,其中包括只用于生产环境的属性。文件名称要遵循application-{profile名}.yml或application-{profile名}.properties。
创建一个名为application-prod.yml的文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/food
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
root: WARN
- 第二种方式仅适用于YAML配置,将特定的profile的属性和非profile的属性都放到application.yml文件中
logging:
level:
root: DEBUG
---
spring:
profiles: prod
datasource:
url: jdbc:mysql://localhost:3306/food
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
root: WARN
通过一组“—”将文件分成两部分,第一部分没有指定spring.profiles,所以如果当前激活的profile没有设置这属性,它就会作为默认值。当prod的profile激活后,原本默认的logging.level.root的属性就会被重写,而且还会连接MySQL数据库。
3.2 激活profile
激活profile的第一种方式可以在application.yml文件中给spring.profiles.active属性赋值 ,但这种方式很不好
spring:
profiles:
active:
- prod
第2中方式(推荐):使用环境变量来设置处于激活状态的profile。在生产环境中可以: % export SPRING_PROFILES_ACTIVE = prod
3.3使用profile条件优化创建bean
正常情况下,不管哪个profile被激活,所有声明的bean都会被创建,假如希望某些bean在某个特定的profile激活情况下才创建,就需要用到@Profile注解,
@Profile({“xxx”,“xx”})表示在xxx profile或xx profile激活下被创建,
@Profile({"!xxx","!xx"})表示在xxx profile和xx profile均没有激活下就被创建,