SpringBoot配置详解
大约 5 分钟
SpringBoot配置详解
配置文件概述
Spring Boot支持多种配置文件格式,主要包括:
- application.properties:传统的属性文件格式
- application.yml:YAML格式,结构更清晰
- application.yaml:YAML格式的另一种扩展名
配置文件优先级
Spring Boot按照以下优先级加载配置文件(数字越小优先级越高):
application.properties配置
基本配置示例
# 服务器配置
server.port=8080
server.servlet.context-path=/api
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
# 日志配置
logging.level.com.example=DEBUG
logging.level.org.springframework=INFO
logging.file.name=logs/application.log
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%napplication.yml配置
基本配置示例
# 服务器配置
server:
  port: 8080
  servlet:
    context-path: /api
# 数据源配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testdb
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
  
  # JPA配置
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true
  
  # Redis配置
  redis:
    host: localhost
    port: 6379
    timeout: 2000ms
# 日志配置
logging:
  level:
    com.example: DEBUG
    org.springframework: INFO
  file:
    name: logs/application.log
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"多环境配置
Profile配置文件
Spring Boot支持通过Profile实现多环境配置:
application.properties          # 默认配置
application-dev.properties      # 开发环境配置
application-test.properties     # 测试环境配置
application-prod.properties     # 生产环境配置或者使用YAML格式:
# application.yml
spring:
  profiles:
    active: dev
---
# 开发环境配置
spring:
  profiles: dev
server:
  port: 8080
  servlet:
    context-path: /api-dev
---
# 测试环境配置
spring:
  profiles: test
server:
  port: 8081
  servlet:
    context-path: /api-test
---
# 生产环境配置
spring:
  profiles: prod
server:
  port: 80
  servlet:
    context-path: /api激活Profile
# 通过命令行参数激活
java -jar app.jar --spring.profiles.active=prod
# 通过环境变量激活
export SPRING_PROFILES_ACTIVE=prod
# 通过application.properties激活
spring.profiles.active=prod自定义配置属性
使用@ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    
    private String name;
    private String version;
    private List<String> servers = new ArrayList<>();
    private Map<String, String> settings = new HashMap<>();
    
    // getters and setters
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getVersion() {
        return version;
    }
    
    public void setVersion(String version) {
        this.version = version;
    }
    
    public List<String> getServers() {
        return servers;
    }
    
    public void setServers(List<String> servers) {
        this.servers = servers;
    }
    
    public Map<String, String> getSettings() {
        return settings;
    }
    
    public void setSettings(Map<String, String> settings) {
        this.settings = settings;
    }
}对应的配置文件:
# application.yml
app:
  name: MyApp
  version: 1.0.0
  servers:
    - http://server1.com
    - http://server2.com
  settings:
    timeout: 5000
    retry: 3启用配置属性处理
@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}使用自定义配置
@Service
public class AppService {
    
    @Autowired
    private AppProperties appProperties;
    
    public void printAppInfo() {
        System.out.println("应用名称: " + appProperties.getName());
        System.out.println("应用版本: " + appProperties.getVersion());
        System.out.println("服务器列表: " + appProperties.getServers());
        System.out.println("配置设置: " + appProperties.getSettings());
    }
}配置元数据
添加配置元数据
在[META-INF/spring-configuration-metadata.json](file:///Users/ldf/app/docs/src/main/resources/META-INF/spring-configuration-metadata.json)中添加配置元数据:
{
  "groups": [
    {
      "name": "app",
      "type": "com.example.config.AppProperties",
      "sourceType": "com.example.config.AppProperties"
    }
  ],
  "properties": [
    {
      "name": "app.name",
      "type": "java.lang.String",
      "description": "应用名称"
    },
    {
      "name": "app.version",
      "type": "java.lang.String",
      "description": "应用版本"
    }
  ]
}使用注解生成元数据
@Component
@ConfigurationProperties(prefix = "app")
@ConstructorBinding
public class AppProperties {
    
    /**
     * 应用名称
     */
    private final String name;
    
    /**
     * 应用版本
     */
    private final String version;
    
    public AppProperties(String name, String version) {
        this.name = name;
        this.version = version;
    }
    
    public String getName() {
        return name;
    }
    
    public String getVersion() {
        return version;
    }
}添加依赖以生成配置元数据:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>外部化配置
命令行参数
java -jar app.jar --server.port=9090 --app.name=ExternalApp环境变量
export SERVER_PORT=9090
export APP_NAME=ExternalApp
java -jar app.jar系统属性
java -Dserver.port=9090 -Dapp.name=ExternalApp -jar app.jarSPRING_APPLICATION_JSON
export SPRING_APPLICATION_JSON='{"server":{"port":9090},"app":{"name":"ExternalApp"}}'
java -jar app.jar配置属性验证
使用Bean Validation
@Component
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
    
    @NotNull
    @NotBlank
    private String name;
    
    @Pattern(regexp = "\\d+\\.\\d+\\.\\d+")
    private String version;
    
    @Min(1)
    @Max(65535)
    private Integer port = 8080;
    
    // getters and setters
}自定义验证
@Component
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
    
    private String name;
    
    @ValidServerList
    private List<String> servers = new ArrayList<>();
    
    // getters and setters
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ServerListValidator.class)
public @interface ValidServerList {
    String message() default "服务器列表格式不正确";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}
public class ServerListValidator implements ConstraintValidator<ValidServerList, List<String>> {
    
    @Override
    public boolean isValid(List<String> servers, ConstraintValidatorContext context) {
        if (servers == null || servers.isEmpty()) {
            return true;
        }
        
        return servers.stream().allMatch(this::isValidUrl);
    }
    
    private boolean isValidUrl(String url) {
        try {
            new URL(url);
            return true;
        } catch (MalformedURLException e) {
            return false;
        }
    }
}加密敏感配置
使用Jasypt加密
添加依赖:
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>配置加密密钥:
# application.properties
jasypt.encryptor.password=mysecretpassword加密敏感信息:
# 使用Jasypt加密工具加密密码
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI \
  input="mypassword" password=mysecretpassword algorithm=PBEWithMD5AndDES使用加密配置:
# application.properties
spring.datasource.password=ENC(加密后的密码)配置刷新
使用@RefreshScope
@RestController
@RefreshScope
public class ConfigController {
    
    @Value("${app.message:Hello World}")
    private String message;
    
    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}添加Actuator依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>启用刷新端点
# application.yml
management:
  endpoints:
    web:
      exposure:
        include: refresh刷新配置
# 刷新配置
curl -X POST http://localhost:8080/actuator/refresh配置最佳实践
1. 分离配置
# application.yml - 通用配置
spring:
  application:
    name: myapp
# application-dev.yml - 开发环境配置
spring:
  datasource:
    url: jdbc:h2:mem:testdb
  jpa:
    hibernate:
      ddl-auto: create-drop
# application-prod.yml - 生产环境配置
spring:
  datasource:
    url: jdbc:mysql://prod-server:3306/mydb
  jpa:
    hibernate:
      ddl-auto: validate2. 使用配置类
@Configuration
@ConfigurationProperties(prefix = "app.database")
public class DatabaseProperties {
    
    private String url;
    private String username;
    private String password;
    
    // getters and setters
}
@Configuration
@EnableConfigurationProperties(DatabaseProperties.class)
public class DatabaseConfig {
    
    @Autowired
    private DatabaseProperties databaseProperties;
    
    @Bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(databaseProperties.getUrl());
        dataSource.setUsername(databaseProperties.getUsername());
        dataSource.setPassword(databaseProperties.getPassword());
        return dataSource;
    }
}3. 配置文档化
/**
 * 应用配置属性
 */
@Component
@ConfigurationProperties(prefix = "app")
@Validated
public class AppProperties {
    
    /**
     * 应用名称,用于标识应用实例
     */
    @NotBlank
    private String name;
    
    /**
     * 应用版本号,格式为X.Y.Z
     */
    @Pattern(regexp = "\\d+\\.\\d+\\.\\d+")
    private String version;
    
    /**
     * 服务器端口,范围1-65535
     */
    @Min(1)
    @Max(65535)
    private Integer port = 8080;
    
    // getters and setters
}通过以上内容,我们可以全面了解Spring Boot的配置机制,包括配置文件格式、多环境配置、自定义属性、外部化配置、加密敏感信息、配置刷新等各个方面。合理的配置管理是构建可维护、可扩展应用的重要基础。
