Spring Boot 3.x 特性-配置与配置源

在 Spring Boot 3.x 中,配置与配置源是构建灵活且可配置的应用程序的重要部分。Spring Boot 提供了多种方式来管理配置,包括使用外部配置文件、环境变量、命令行参数等。以下是关于配置与配置源的详细指南。

1. 外部配置文件

Spring Boot 支持多种外部配置文件格式,如 application.propertiesapplication.yml。这些文件通常放在 src/main/resources 目录下。

application.properties 示例
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
application.yml 示例
server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: password

2. 配置类和 @Value 注解

你可以使用 @ConfigurationProperties@Value 注解将配置属性绑定到 Java 类。

使用 @ConfigurationProperties

首先,创建一个配置类并使用 @ConfigurationProperties 注解。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
    private String url;
    private String username;
    private String password;

    // Getters and setters
}

然后,在主应用程序类中启用配置属性支持。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(DataSourceProperties.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
使用 @Value

你还可以直接在字段上使用 @Value 注解。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    @Value("${server.port}")
    private int serverPort;

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    // Getters and other methods
}

3. 环境变量和命令行参数

Spring Boot 允许通过环境变量和命令行参数来覆盖默认配置。

使用环境变量

你可以在启动应用程序时设置环境变量,例如:

export SERVER_PORT=8081
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/anotherdb
使用命令行参数

你可以在启动应用程序时传递命令行参数,例如:

java -jar myapp.jar --server.port=8081 --spring.datasource.url=jdbc:mysql://localhost:3306/anotherdb

4. 配置优先级

Spring Boot 有一套配置优先级机制,从高到低如下:

  1. 命令行参数
  2. SPRING_APPLICATION_JSON 中的属性
  3. ServletConfig 参数
  4. ServletContext 参数
  5. JNDI 属性
  6. Java 系统属性 (System.getProperties())
  7. 操作系统环境变量
  8. RandomValuePropertySource 配置的属性
  9. 应用程序外部的 application-{profile}.propertiesapplication.yml
  10. 应用程序内部的 application-{profile}.propertiesapplication.yml
  11. @PropertySource 注解的属性
  12. 默认属性(使用 SpringApplication.setDefaultProperties 设置)

5. 自定义配置源

如果你需要从自定义位置加载配置,可以实现 PropertySource 接口。

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfig {
}

或者创建一个自定义 PropertySource 实现:

import org.springframework.core.env.PropertySource;

public class MyPropertySource extends PropertySource<String> {

    public MyPropertySource(String name) {
        super(name);
    }

    @Override
    public Object getProperty(String name) {
        // 自定义逻辑
        return null;
    }
}

然后将其注册到 Environment 中:

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class MyPropertySourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        environment.getPropertySources().addLast(new MyPropertySource("myPropertySource"));
    }
}

application.properties 中注册这个初始化器:

context.initializer.classes=com.example.MyPropertySourceInitializer

总结

Spring Boot 3.x 提供了多种方式来管理和使用配置,使得应用程序能够灵活应对不同的部署环境和需求。通过合理地使用外部配置文件、环境变量、命令行参数以及自定义配置源,你可以构建出更加健壮和灵活的应用程序。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值