Spring Boot 入门 - 基础篇(14)- 参数设置

[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]
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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值