Spring Boot 使用配置文件来管理应用程序的配置信息。这些配置文件可以用来设置数据库连接、服务器端口、日志级别等参数。Spring Boot 支持多种格式的配置文件,最常见的是 application.properties
和 application.yml
以及 application.yaml
。
Spring Boot 会按照以下顺序加载配置文件(优先级从高到低):
config
文件夹:/config/application.properties
或 /config/application.yml
或 /config/application.yaml
/application.properties
或 /application.yml
或 /application.yaml
config
文件夹:classpath:/config/application.properties
或 classpath:/config/application.yml
或 classpath:/config/application.yaml
classpath:/application.properties
或 classpath:/application.yml
或 classpath:/application.yaml
高优先级的配置会覆盖低优先级的配置。
注
同目录下的文件优先级如下:
application.properties
>application.yml
>application.yaml
。
这是传统的 .properties
格式,键值对形式:
properties# 设置服务器端口 server.port=8080 # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # 日志级别配置 logging.level.org.springframework=DEBUG logging.level.com.example=INFO # 自定义属性 app.name=MyApp app.version=1.0.0
这是 YAML 格式的配置文件,支持层级结构,更简洁易读:
yaml# 设置服务器端口
server:
port: 8080
# 数据库连接配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
# 日志级别配置
logging:
level:
org.springframework: DEBUG
com.example: INFO
# 自定义属性
app:
name: MyApp
version: 1.0.0
propertiesserver.port=8080
propertiesserver.servlet.context-path=/myapp
propertiesspring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
propertiesspring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.minimum-idle=5
propertieslogging.level.root=WARN logging.level.org.springframework=DEBUG logging.level.com.example=INFO
propertieslogging.file.name=logs/app.log logging.file.path=/var/logs/
propertiesmanagement.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
propertiesspring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
你可以在配置文件中定义自己的属性,并通过 @Value
或 @ConfigurationProperties
注解注入到代码中。
propertiesapp.name=MyApp app.version=1.0.0
在代码中使用:
java@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
或者使用 @ConfigurationProperties
:
java@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
// getters and setters...
}
Spring Boot 支持多环境配置,可以通过 application-{profile}.properties
或 application-{profile}.yml
文件来区分不同环境的配置。例如:
application-dev.properties
:开发环境application-prod.properties
:生产环境激活某个环境的配置:
propertiesspring.profiles.active=dev
或者在运行时通过命令行参数指定:
bashjava -jar myapp.jar --spring.profiles.active=prod
如果需要保护敏感信息(如数据库密码),可以使用工具(如 Jasypt)对配置文件中的值进行加密。
示例:
propertiesspring.datasource.password=ENC(加密后的字符串)
然后在启动时提供解密密钥:
bashjava -Djasypt.encryptor.password=mySecretKey -jar myapp.jar
如果你不想使用默认的 application.properties
或 application.yml
,可以通过以下方式指定自定义配置文件:
bashjava -jar myapp.jar --spring.config.location=classpath:/custom-config.properties
在主类中通过 SpringApplication.setDefaultProperties()
设置:
java@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
Properties props = new Properties();
props.setProperty("spring.config.location", "classpath:/custom-config.properties");
app.setDefaultProperties(props);
app.run(args);
}
}