文章目录

  • Springboot的配置文件及属性注入
  • 全局配置文件
  • 属性注入
  • 常用属性注入注解
  • 批量注入
  • 自定义注解类
  • **编写配置文件时有提示**
  • **第三方配置**
  • 松散绑定
  • @ConfigurationProperties vs @Value


Springboot的配置文件及属性注入

全局配置文件

Spring boot使用一个application.properties或者application.yml的文件作为全局配置文件

它可以放在 classpath:/,classpath:/config/,file:./,file:./config/

对应项目的位置及优先级(按照优先级由高到低的顺序):

-file:./config/        项目根目录config文件夹下
-file:./               项目根目录下
-classpath:/config/    resources下的config文件夹下
-classpath:/           resources下

Spring boot会加载四个位置的全部配置文件,多个配置文件配置可以互补,如果同一个配置在多个文件中配置,默认使用优先级高的配置。

如果配置文件名称不叫application.properties或application.yml,可以公国启动参数来指定配置文件名,例如:使用myproject作用配置文件名

$ java -jar myproject.jar --spring.config.name=myproject

或者可以使用 --spring.config.location=配置文件位置指定使用指定位置的配置文件。

.properties和.yml的优先级

Spring boot在2.4版本之前: .properties > .yml

Springboot 在2.4版本之后:.yml > .properties

属性注入

常用属性注入注解

@Configuration : 声明一类为配置类

@Bean:声明在方法上,将返回值加入到bean容器中

@Value:属性注入

@ConfigurationProperties(prefix="jdbc"): 批量属性注入

@PropertySource("classpath:/jdbc.properties"): 指定外部属性文件,添加在类上

批量注入

自定义注解类

使用@ConfigurationProperties可以实现自定义属性的批量注入,它需要与@EnableConfigurationProperties配合使用。@EnableConfigurationProperties用于启用对@ConfigurationProperties的支持。如下例:

@EnableConfigurationProperties(JdbcProperties.class)
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    // 。。。省略setter、getter
}
jdbc.driver-class-name=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot
jdbc.username=root
jdbc.password=root
编写配置文件时有提示

添加依赖spring-boot-configuration-processor

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

编译后,即可在编写配置文件时出现提示

第三方配置

@ConfigurationPropertis还可以在公共的@Bean方法上使用,实现属性与第三方组件绑定

例如:

public class CustomComponent {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "CustomComponent{" +
                "name='" + name + '\'' +
                '}';
    }
}
@Configuration
public class CustomConfig {


    @Bean
    @ConfigurationProperties(prefix = "custom")
    public CustomComponent customComponent() {
        return new CustomComponent();
    }
}

application.properties文件配置

custom.name=zhangsan

测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomConfigTest {

    @Autowired
    private CustomComponent customComponent;

    @Test
    public void test1() {
        System.out.println(customComponent);
    }

}

结果

CustomComponent{name='zhangsan'}

松散绑定

Spring boot使用一些宽松的规则将环境属性绑定到@ConfigurationProperties bean上,使环境属性名和bean属性名之间不需要完全匹配。

支持模式有:羊肉串模式case(推荐使用),标准驼峰模式,下划线模式,大写下划线模式(系统环境变量推荐使用)。

@ConfigurationProperties vs @Value

特征

@ConfigurationProperties

@Value

宽松的绑定

Y

Limited

元数据支持

Y

N

Spel表达式

N

Y

应用场景

批量属性绑定

单个属性绑定