Spring Boot 应用配置小贴士
本篇文章常用功能和配置为主,简单记录一些 Spring Boot 应用的配置细节。
application.properties
application.properties
或 application.yml
是 Spring Boot 应用的基础配置形式,两种格式只是形式不同,配置的内容是一样的,由于我个人的使用习惯,就只以 properties 格式为例了。
Web 应用常用配置
# Web
server.port=8080
server.address=localhost
server.servlet.context-path=/spring-demo
# Application Base
spring.application.name=spring-demo
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/spring_demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## JPA
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
# Logging
logging.config=classpath:logback-spring.xml
- 基础
配置项 | 说明 |
---|---|
server.port | 服务端口 |
server.address | 绑定地址 |
server.servlet.context-path | 上下文路径 |
spring.application.name | 应用名称 |
- 数据库
配置项 | 说明 |
---|---|
spring.datasource.url | 数据库链接 |
spring.datasource.username | 数据库用户名 |
spring.datasource.password | 数据库密码 |
spring.datasource.driver-class-name | 数据库驱动类 |
spring.jpa.generate-ddl | JPA 层面控制是否生成 DDL 语句 |
spring.jpa.hibernate.ddl-auto | Hibernate DDL 自动操作 |
spring.jpa.show-sql | Hibernate 显示 SQL 日志 |
spring.jpa.properties.hibernate.format_sql | Hibernate SQL 日志自动格式化为可读性更好的格式 |
提示
spring.jpa.generate-ddl
从 JPA 层面控制是否生成 DDL 语句,而 spring.jpa.hibernate.ddl-auto
控制 Hibernate 的 DDL 自动操作,两者的区别在于前者是 JPA 的配置,后者是 Hibernate 实现的配置。
- 日志
配置项 | 说明 |
---|---|
logging.config | 指定日志配置文件 |
配置文件的加载
默认情况下,Spring Boot 会根据以下优先顺序加载配置文件:
file:./config/
file:./
classpath:/config/
classpath:/
你可以通过命令行参数覆盖使用的配置文件或配置:
# 使用 `--spring.config.location` 指定配置文件路径,多个路径使用逗号分隔
# 实际也是使用双减号覆盖了 spring.config.location 的默认值
java -jar myproject.jar --spring.config.location=/path/to/custom/application.properties
# 使用双减号覆盖或新增需要的配置项
java -jar myproject.jar --server.port=8081
自定义常量配置
新建常量配置
custom.name=Name
custom.value=100
custom.text=${custom.name} is ${custom.value}
custom.random=${random.value}
custom.uuid=${random.uuid}
使用自定义键值对定义常量,可以在配置内互相引用已定义的配置项。
提示
${random.*}
是 Spring Boot 内置的随机生成器,可以生成随机数、UUID 等。
random.value
生成一个随机 32 位 MD5 字符串random.int
生成一个随机短整型,可以加[min,max]
限定范围(区间左闭右开)或(max)
限定最大值,实际是省略的[0,max]
random.long
生成一个随机长整型,类似random.int
random.uuid
生成一个随机 UUID
配置换行
如果某个配置项很长很长,为了可读性,可以对其手动换行:
some.long.config=This is a \
very \
very long \
configuration
在 Java 代码中使用
// Spring Bean 类内
@Value("${custom.name}")
private String name;
@Value("${custom.value}")
private int value;
// ...
不逐个绑定,直接绑定到一个 Bean
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
private String name;
private int value;
// getter & setter
}
提示
prefix
指定前缀,会自动绑定 custom.name
到 name
,custom.value
到 value
。Bean 字段和配置项是宽松对应的,不区分大小写,减号和下划线等(例如字段为 something
,配置中可以写 some-thing
some_thing
someThing
)。
使用自定义配置文件
有时我们希望有些配置项不放在 application.properties
中,可以新建一个配置文件,然后再在 Java 代码中引用自定义配置。
# custom.properties
custom.name=My Name
custom.value=812
@PropertySource("classpath:custom.properties")
@ConfigurationProperties(prefix = "custom")
// other code
使用 @PropertySource
注解指定类注入的自定义配置文件路径。
logback
默认情况下,Spring Boot 使用 Logback 作为日志框架,你可以在 resources
目录下新建 logback.xml
文件进行日志配置。但更建议使用 logback-spring.xml
这样的文件名。
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<property name="log.path" value="logs/"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%green([%d{HH:mm:ss.SSS}]) %highlight(%-5level) %cyan(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>[%date] %level [%thread] %logger{36} [%file : %line] %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
这个示例配置展示了一个简单的 Logback 配置,包含了控制台标准输出和滚动文件输出两种方式(同时启用)。两个 appender
分别是 console
和 file
,root
标签指定了日志输出级别和输出方式(引用两个 appender
)。