四,Spring参数配置

87 阅读2分钟

一,参数配置化

我们编写的一些工具类,访问web的url,key密钥等都是如下图编写在java类中的。

image-20231102223243678

SpringBoot提供了@Value注解来解决这个方法,通过这个注解将这些需要维护的数据值,统一编写的配置文件之中。

image-20231102223340744

  • @Value注解通常用于外部配置的属性注入,具体用法:@Value("${配置文件中的key}")

使用value注解需要注意:

  1. 不能用static,final修饰变量
  2. 必须使用DI注入而不是手动创建对象
  3. 在类上加上@Component这种类型的注解,将对象较给IOC容器创建
  4. 记得类中写上get,Set,有参无参构造

二,yml配置文件

SpringBoot提供了多种配置文件类型:

  • application**.properties**

    server.port=8080
    server.address=127.0.0.1
    
  • application**.yml**

    server:
    	port: 8080
    	address: 127.0.0.1
    
  • application**.yaml**

    server:
    	port: 8080
    	address: 127.0.0.1
    

yml基本语法:

image-20231102224935951

yml数据格式:

  • 对象/Map集合:

    user:
    	name: zhansan
    	age: 18
    	password: 123456
    
  • 数值/List/Set集合:

    hobby:
    	-java
    	-game
    	-sport
    

三,批量参数注入

在一中针对每个属性都需要配置value注解里面的key的全名,很繁琐。

解决方法:

  • 使用@ConfigurationProperties注解,这样就只需要加上前缀名即可自动配置。
  • @ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。

image-20231102225646252

需要注意,@Value里面的注意点@ConfigurationProperties也需要遵守。

四,加载多个配置文件实现多环境切换

在SpringBoot中,除了application.propertie,我们新建的其他配置文件的文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识(不一定是.properties文件,也可以是.yml

  • 其对应的{profile}值是开发者自定义的(如dev,product),在项目启动的时候,只需要添加对应的参数,Springboot就会去读取该配置文件了。
  • 具体profile的配置在application.properties文件中通过spring.profiles.active属性来设置。

用法:

  1. 创建application-dev.yamlapplication-product.yaml和默认的application.yaml文件

    image-20231103161705571

  2. application.yml文件中编写:

    spring:
      profiles:
        active: @profile.active@
    
  3. pom.xml中编写:

    <!--定义三种开发环境-->
    <profiles>
     <profile>
         <!--不同环境的唯一id-->
         <id>dev</id>
         <activation>
             <!--默认激活开发环境-->
             <activeByDefault>true</activeByDefault>
         </activation>
         <properties>
             <!--profile.active对应application.yml中的@profile.active@-->
             <profile.active>dev</profile.active>
         </properties>
     </profile>
     <!--测试环境-->
     <profile>
         <id>test</id>
         <properties>
             <profile.active>test</profile.active>
         </properties>
     </profile>
     <!--生产环境-->
     <profile>
         <id>prod</id>
         <properties>
             <profile.active>prod</profile.active>
         </properties>
     </profile>
     <profile>
         <id>local</id>
         <properties>
             <profile.active>local</profile.active>
         </properties>
     </profile>
    </profiles>
    
    <build>
     <!--开启资源拦截-->
     <resources>
         <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering>
         </resource>
     </resources>
    </build>
    
  4. 在maven配置文件中切换配置文件

    image-20231103161939766

注意要点:

  1. <env>里面的值需要跟application-{profile}中的profil保持一致

  2. @env需要的也是env标签!

  3. 每次切换配置都需要刷新一下maven配置文件

  4. 通过import实现druid配置文件载入

    spring:
      config:
        import: classpath:application-druid.yml