[b](1)读取优先顺序[/b]
a - 命令行参数 --key=value
[quote]$ mvn spring-boot:run -Drun.arguments="--server.port=9090,--server.context-path=/test"
$ java -jar target/xxx.jar --server.port=9090 --server.context-path=/test[/quote]
b - JVM参数 -Dkey=value
[quote]$ mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Dserver.port=9090 -Dserver.context-path=/test"
$ java -jar target/xxx.jar -Dserver.port=9090 -Dserver.context-path=/test[/quote]
c - 环境变量
d - application-{profile}.properties
e - application.properties
*** 还有很多地方可以设置,以上只是常用的!
[b](2)文件格式[/b]
同时支持properties或YAML。
application.yml
[quote]prefix:
stringProp1: propValue1
stringProp2: propValue2
intProp1: 10
listProp:
- listValue1
- listValue2
mapProp:
key1: mapValue1
key2: mapValue2[/quote]
application.properties
[quote]prefix.stringProp1=propValue1
prefix.stringProp2=propValue2
prefix.intProp1=10
prefix.listProp[0]=listValue1
prefix.listProp[1]=listValue2
prefix.mapProp.key1=mapValue1
prefix.mapProp.key2=mapValue2[/quote]
变量可以嵌套使用
[quote]project.base-dir=file:///D:/springbootsample/spring-boot-demo1
spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/[/quote]
[b](3)文件位置[/b]
src/main/resources/config/application.properties
src/main/resources/application.properties
*** 也可以通过@PropertySource("classpath:config.properties") 来读入任意其他设置文件。
[b](4)多环境配置[/b]
通过设置参数“spring.profiles.active”即可切换设置文件。
[quote]application.properties
application-develop.properties
application-test.properties
application-product.properties[/quote]
[b](5)设置值的Key支持Relaxed binding[/b]
以下四种写法都是可以的。
[list]
[*]person.firstName 标准骆驼式语法
[*]person.first-name 横线 多用于.properties 或 .yml
[*]person.first_name 下划线 多用于.properties 或 .yml
[*]PERSON_FIRST_NAME 大写 多用于环境变量
[/list]
[b](6)读取配置值[/b]
[b]a - @Value()[/b]
@Value支持二元操作符并支持嵌套:
[quote]#{expression?:default value}
${property:default value}[/quote]
[b]b - @ConfigurationProperties[/b]
[b]c - Environment[/b]
[b](7)常用设置[/b]
官方文档里有详细的设置说明,用什么设置什么即可。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
[quote]server.port=
server.context-path=
server.session.timeout=
logging.level=
spring.messages.cache-seconds=
spring.thymeleaf.cache=
spring.velocity.cache=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=
spring.mvc.throw-exception-if-no-handler-found=[/quote]
a - 命令行参数 --key=value
[quote]$ mvn spring-boot:run -Drun.arguments="--server.port=9090,--server.context-path=/test"
$ java -jar target/xxx.jar --server.port=9090 --server.context-path=/test[/quote]
b - JVM参数 -Dkey=value
[quote]$ mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Dserver.port=9090 -Dserver.context-path=/test"
$ java -jar target/xxx.jar -Dserver.port=9090 -Dserver.context-path=/test[/quote]
c - 环境变量
d - application-{profile}.properties
e - application.properties
*** 还有很多地方可以设置,以上只是常用的!
[b](2)文件格式[/b]
同时支持properties或YAML。
application.yml
[quote]prefix:
stringProp1: propValue1
stringProp2: propValue2
intProp1: 10
listProp:
- listValue1
- listValue2
mapProp:
key1: mapValue1
key2: mapValue2[/quote]
application.properties
[quote]prefix.stringProp1=propValue1
prefix.stringProp2=propValue2
prefix.intProp1=10
prefix.listProp[0]=listValue1
prefix.listProp[1]=listValue2
prefix.mapProp.key1=mapValue1
prefix.mapProp.key2=mapValue2[/quote]
变量可以嵌套使用
[quote]project.base-dir=file:///D:/springbootsample/spring-boot-demo1
spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/[/quote]
[b](3)文件位置[/b]
src/main/resources/config/application.properties
src/main/resources/application.properties
*** 也可以通过@PropertySource("classpath:config.properties") 来读入任意其他设置文件。
[b](4)多环境配置[/b]
通过设置参数“spring.profiles.active”即可切换设置文件。
[quote]application.properties
application-develop.properties
application-test.properties
application-product.properties[/quote]
[b](5)设置值的Key支持Relaxed binding[/b]
以下四种写法都是可以的。
[list]
[*]person.firstName 标准骆驼式语法
[*]person.first-name 横线 多用于.properties 或 .yml
[*]person.first_name 下划线 多用于.properties 或 .yml
[*]PERSON_FIRST_NAME 大写 多用于环境变量
[/list]
[b](6)读取配置值[/b]
[b]a - @Value()[/b]
public class SamplePropertyLoading {
@Value("${prefix.stringProp1}")
private String stringProp1;
@Value("${prefix.stringProp2}")
private String stringProp2;
@Value("${prefix.intProp1}")
private Integer intProp1;
@Value("${prefix.listProp}")
private List<String> listProp;
@Value("${prefix.mapProp}")
private Map<String, String> mapProp;
// ...
}@Value支持二元操作符并支持嵌套:
[quote]#{expression?:default value}
${property:default value}[/quote]
[b]b - @ConfigurationProperties[/b]
@Component
@ConfigurationProperties(prefix = "prefix")
public class SampleProperty {
private String stringProp1;
private String stringProp2;
private Integer intProp1;
private List<String> listProp;
private Map<String, String> mapProp;
// ...
}[b]c - Environment[/b]
@Autowired
private Environment env;
String errorPath = env.getProperty("server.error.path");[b](7)常用设置[/b]
官方文档里有详细的设置说明,用什么设置什么即可。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
[quote]server.port=
server.context-path=
server.session.timeout=
logging.level=
spring.messages.cache-seconds=
spring.thymeleaf.cache=
spring.velocity.cache=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=
spring.mvc.throw-exception-if-no-handler-found=[/quote]
本文介绍了Spring Boot中配置加载的优先级、支持的文件格式、配置文件的位置、多环境配置切换、支持的Relaxedbinding特性及配置读取方式。
2万+

被折叠的 条评论
为什么被折叠?



