`
rensanning
  • 浏览: 3514980 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37500
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604395
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678169
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87329
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399865
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69086
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90519
社区版块
存档分类
最新评论

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

 
阅读更多
(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=
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics