(1)读取优先顺序
a - 命令行参数 --key=value
引用
$ 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
b - JVM参数 -Dkey=value
引用
$ 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
c - 环境变量
d - application-{profile}.properties
e - application.properties
*** 还有很多地方可以设置,以上只是常用的!
(2)文件格式
同时支持properties或YAML。
application.yml
引用
prefix:
stringProp1: propValue1
stringProp2: propValue2
intProp1: 10
listProp:
- listValue1
- listValue2
mapProp:
key1: mapValue1
key2: mapValue2
application.properties
引用
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
变量可以嵌套使用
引用
project.base-dir=file:///D:/springbootsample/spring-boot-demo1
spring.thymeleaf.prefix=${project.base-dir}/src/main/resources/templates/
(3)文件位置
src/main/resources/config/application.properties
src/main/resources/application.properties
*** 也可以通过@PropertySource("classpath:config.properties") 来读入任意其他设置文件。
(4)多环境配置
通过设置参数“spring.profiles.active”即可切换设置文件。
引用
application.properties
application-develop.properties
application-test.properties
application-product.properties
(5)设置值的Key支持Relaxed binding
以下四种写法都是可以的。
- person.firstName 标准骆驼式语法
- person.first-name 横线 多用于.properties 或 .yml
- person.first_name 下划线 多用于.properties 或 .yml
- PERSON_FIRST_NAME 大写 多用于环境变量
(6)读取配置值
a - @Value()
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支持二元操作符并支持嵌套:
引用
#{expression?:default value}
${property:default value}
b - @ConfigurationProperties
@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;
// ...
}
c - Environment
@Autowired
private Environment env;
String errorPath = env.getProperty("server.error.path");
(7)常用设置
官方文档里有详细的设置说明,用什么设置什么即可。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
引用
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=
分享到:
相关推荐
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...
赠送jar包:spring-boot-configuration-processor-2.6.3.jar; 赠送原API文档:spring-boot-configuration-processor-2.6.3-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.6.3-sources.jar; ...
赠送jar包:spring-boot-configuration-processor-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-configuration-processor-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:mybatis-spring-boot-autoconfigure-1.3.2.jar; 赠送原API文档:mybatis-spring-boot-autoconfigure-1.3.2-javadoc.jar; 赠送源代码:mybatis-spring-boot-autoconfigure-1.3.2-sources.jar; 赠送...
赠送jar包:spring-boot-configuration-processor-2.5.6.jar; 赠送原API文档:spring-boot-configuration-processor-2.5.6-javadoc.jar; 赠送源代码:spring-boot-configuration-processor-2.5.6-sources.jar; ...
赠送jar包:spring-boot-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-autoconfigure-2.3.12.RELEASE-sources.jar; 赠送...
赠送jar包:druid-spring-boot-starter-1.1.10.jar; 赠送原API文档:druid-spring-boot-starter-1.1.10-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.10-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:aliyun-sms-spring-boot-starter-2.0.2.jar 赠送原API文档:aliyun-sms-spring-boot-starter-2.0.2-javadoc.jar 赠送源代码:aliyun-sms-spring-boot-starter-2.0.2-sources.jar 包含翻译后的API文档...
赠送jar包:oss-spring-boot-starter-1.0.3.jar; 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar; 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar; 赠送Maven依赖信息文件:oss-spring-boot...
赠送jar包:nacos-config-spring-boot-autoconfigure-0.2.7.jar; 赠送原API文档:nacos-config-spring-boot-autoconfigure-0.2.7-javadoc.jar; 赠送源代码:nacos-config-spring-boot-autoconfigure-0.2.7-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:dynamic-datasource-spring-boot-starter-3.4.1.jar; 赠送原API文档:dynamic-datasource-spring-boot-starter-3.4.1-javadoc.jar; 赠送源代码:dynamic-datasource-spring-boot-starter-3.4.1-sources...
赠送jar包:druid-spring-boot-starter-1.2.8.jar; 赠送原API文档:druid-spring-boot-starter-1.2.8-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.2.8-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:druid-spring-boot-starter-1.1.9.jar; 赠送原API文档:druid-spring-boot-starter-1.1.9-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.1.9-sources.jar; 赠送Maven依赖信息文件:druid-...
赠送jar包:spring-boot-admin-server-ui-2.5.2.jar; 赠送原API文档:spring-boot-admin-server-ui-2.5.2-javadoc.jar; 赠送源代码:spring-boot-admin-server-ui-2.5.2-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:oss-spring-boot-starter-1.0.3.jar 赠送原API文档:oss-spring-boot-starter-1.0.3-javadoc.jar 赠送源代码:oss-spring-boot-starter-1.0.3-sources.jar 包含翻译后的API文档:oss-spring-boot-...
mybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jarmybatis-spring-boot-starter-2.1.3.jar
spring-boot项目pom.xm中parent依赖文件spring-boot-starter-parent-1.5.13.RELEASE.pom