Spring Boot 外部化配置总结
Externalized Configuration
更多信息参考:7.2 Externalized Configuration
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, and command-line arguments.
Property values can be injected directly into your beans by
- using the
@Value
annotation, accessed through Spring’sEnvironment
abstraction,- or be bound to structured objects through
@ConfigurationProperties
.Spring Boot uses a very particular
PropertySource
order that is designed to allow sensible overriding of values. Later property sources can override the values defined in earlier ones. Sources are considered in the following order:
- Default properties (specified by setting
SpringApplication.setDefaultProperties
).@PropertySource
annotations on your@Configuration
classes.- ⭐️ Config data (such as
application.properties
files).- A
RandomValuePropertySource
that has properties only inrandom.*
.- ⭐️ OS environment variables (
System.getenv()
).- ⭐️ Java System properties (
System.getProperties()
) (that is, arguments starting with-D
, such as-Dspring.profiles.active=prod
).- ⭐️ JNDI attributes from
java:comp/env
.ServletContext
init parameters.ServletConfig
init parameters.- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property).- ⭐️ Command line arguments. (that is, arguments starting with
--
, such as--server.port=9000
)properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.@TestPropertySource
annotations on your tests.- Devtools global settings properties in the
$HOME/.config/spring-boot
directory when devtools is active.
⭐️ Config data files are considered in the following order:
- Application properties packaged inside your jar (
application.properties
and YAML variants).- Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants).- Application properties outside of your packaged jar (
application.properties
and YAML variants).- Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants).
It is recommended to stick with one format for your entire application. If you have configuration files with both
.properties
and.yml
format in the same location,.properties
takes precedence.
External Application Properties
7.2.3. External Application Properties
Spring Boot will automatically find and load
application.properties
andapplication.yaml
files from the following locations when your application starts:
- From the classpath
- The classpath root
- The classpath
/config
package- From the current directory
- The current directory
- The
config/
subdirectory in the current directory- Immediate child directories of the
config/
subdirectoryThe list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as
PropertySources
to the SpringEnvironment
.
spring.config.name
修改配置文件名称:
If you do not like
application
as the configuration file name, you can switch to another file name by specifying aspring.config.name
environment property.For example, to look for
myproject.properties
andmyproject.yaml
files you can run your application as follows:
1 java -jar myproject.jar --spring.config.name=myproject
spring.config.location
引用外部配置文件:
You can also refer to an explicit location by using the
spring.config.location
environment property. This property accepts a comma-separated list of one or more locations to check.The following example shows how to specify two distinct files:
1
2
3 java -jar myproject.jar --spring.config.location=\
optional:classpath:/default.properties,\
optional:classpath:/override.propertiesUse the prefix
optional:
if the locations are optional and you do not mind if they do not exist.
配置嵌入式服务器
Spring Boot 集成了 Tomcat、Jetty 和 Undertow,极大便利了项目部署。下面介绍一些常用配置:
1 | server.port=8080 # Server HTTP port. |
Tomcat
URI 编码配置:
1 | server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI. |
代理配置:
1 | server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR` |
Socket 连接限制及等待超时时间:
1 | server.tomcat.max-connections= # Maximum number of connections that the server will accept and process at any given time. |
业务线程池调优:
1 | server.tomcat.max-threads=0 # Maximum amount of worker threads. Default 200. |
Undertow
1 | # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程 |
https://www.cnblogs.com/duanxz/p/9337022.html
属性注入
Common Application Properties
Appendix A: Common Application Properties
参考
《Spring Boot in Action》