# Spring Boot 应用配置小贴士
目录
application.properties
application.properties
或 application.yml
是 Spring Boot 应用的基础配置形式,两种格式只是形式不同,配置的内容是一样的,由于我个人的使用习惯,就只以 properties 格式为例了。
Web 应用常用配置
展开查看示例配置
# Webserver.port=8080server.address=localhostserver.servlet.context-path=/spring-demo
# Application Basespring.application.name=spring-demo
# Databasespring.datasource.url=jdbc:mysql://localhost:3306/spring_demospring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver## JPAspring.jpa.generate-ddl=truespring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=true
# Logginglogging.config=classpath:logback-spring.xml
配置文件的加载
默认情况下,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=Namecustom.value=100custom.text=${custom.name} is ${custom.value}custom.random=${random.value}custom.uuid=${random.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}
使用自定义配置文件
有时我们希望有些配置项不放在 application.properties
中,可以新建一个配置文件,然后再在 Java 代码中引用自定义配置。
custom.name=My Namecustom.value=812
@PropertySource("classpath:custom.properties")@ConfigurationProperties(prefix = "custom")// other code
使用 @PropertySource
注解指定类注入的自定义配置文件路径。
@PropertySource("classpath:custom.properties")
这样的绑定其实是限定于这一个文件的(位于 jar 包或 resource 里),无法方便的自定义配置。但我们又想分离应用配置和业务配置,要怎么办呢?
可以像这样建立多个配置文件,然后在 Spring 主配置内使用引入:
# ...other configspring.config.import=optional:classpath:service.properties, optional:file:./service.properties
service.name=My Serviceservice.version=1.0.0
spring.config.import
能够在配置文件内引入其他配置,而 optional:
前缀表示如果引入的配置文件不存在,不会报错。使用逗号分隔多个配置位置,加载时会按照从左到右加载,后面的配置优先级更高,会覆盖前面的配置,这样就轻松实现了 配置分离+优先级加载。
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
)。