Spring Boot(五) 之使用配置属性

本文详细讲解如何在SpringBoot中使用配置属性和profile,包括自定义配置、配置文件管理、profile环境切换及其在bean创建中的应用。通过实例演示了如何根据不同环境(如开发、生产)调整配置,提升部署灵活性。
摘要由CSDN通过智能技术生成

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中。

  1. 将配置放到一个特定的类中,configNum和configStr两个配置属性
    需要在类上加上@ConfigurationProperties注解
@Component
@ConfigurationProperties(prefix = "bean.config")
@Data
public class ConfigBean {
    private int configNum = 20;
    private String configStr = "This is default String";
}
  1. 注入到任意需要这些属性的其他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的属性

  1. 第一种方式:创建另外一个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
  1. 第二种方式仅适用于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 profilexx profile激活下被创建,
@Profile({"!xxx","!xx"})表示在xxx profile和xx profile均没有激活下就被创建,

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子时不睡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值