Spring Boot 支持多種外部配置方式
http://blog.csdn.net/isea533/article/details/50281151
這些方式優先級如下:
- 命令行參數
- 來自
java:comp/env
的JNDI屬性 - Java系統屬性(
System.getProperties()
) - 操作系統環境變量
RandomValuePropertySource
配置的random.*
屬性值jar
包外部的application-{profile}.properties
或application.yml
(帶spring.profile
)配置文件jar
包內部的application-{profile}.properties
或application.yml
(帶spring.profile
)配置文件jar
包外部的application.properties
或application.yml
(不帶spring.profile
)配置文件jar
包內部的application.properties
或application.yml
(不帶spring.profile
)配置文件@Configuration
注解類上的@PropertySource
- 通過
SpringApplication.setDefaultProperties
指定的默認屬性
命令行參數
通過Java -jar app.jar --name="Spring" --server.port=9090
方式來傳遞參數。
參數用--xxx=xxx
的形式傳遞。
可以使用的參數可以是我們自己定義的,也可以是Spring Boot中默認的參數。
很多人可能會關心如web端口如何配置這樣的問題,這些都是Spring Boot中提供的參數,部分可用參數如下:
# LOGGING logging.path=/var/logs logging.file=myapp.log logging.config= # location of config file (default classpath:logback.xml for logback) logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF) # EMBEDDED SERVER CONFIGURATION (ServerProperties) server.port=8080 server.address= # bind to a specific NIC server.session-timeout= # session timeout in seconds server.context-parameters.*= # Servlet context init parameters, e.g. server.context-parameters.a=alpha server.context-path= # the context path, defaults to '/' server.servlet-path= # the servlet path, defaults to '/'
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
更多常見的應用屬性請瀏覽這里
注意:命令行參數在app.jar
的后面!
可以通過SpringApplication.setAddCommandLineProperties(false)
禁用命令行配置。
Java系統屬性
注意Java系統屬性位置java -Dname="isea533" -jar app.jar
,可以配置的屬性都是一樣的,優先級不同。
例如java -Dname="isea533" -jar app.jar --name="Spring!"
中name
值為Spring!
操作系統環境變量
配置過JAVA_HOME的應該都了解這一個。
這里需要注意的地方,有些OS可以不支持使用.
這種名字,如server.port
,這種情況可以使用SERVER_PORT
來配置。
具體名字如何匹配,看本文后面。
RandomValuePropertySource
系統中用到隨機數的地方,例如:
my.secret=${random.value} my.number=${random.int} my.bignumber=${random.long} my.number.less.than.ten=${random.int(10)} my.number.in.range=${random.int[1024,65536]}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
random.int*
支持value
參數和,max
參數,當提供max
參數的時候,value
就是最小值。
應用配置文件(.properties或.yml)
在配置文件中直接寫:
name=Isea533 server.port=8080
- 1
- 2
- 1
- 2
.yml
格式的配置文件如:
name: Isea533 server: port: 8080
- 1
- 2
- 3
- 1
- 2
- 3
當有前綴的情況下,使用.yml
格式的配置文件更簡單。關於.yml
配置文件用法請看這里
注意:使用.yml
時,屬性名的值和冒號中間必須有空格,如name: Isea533
正確,name:Isea533
就是錯的。
屬性配置文件的位置
spring會從classpath下的/config
目錄或者classpath的根目錄查找application.properties
或application.yml
。
/config
優先於classpath根目錄
@PropertySource
這個注解可以指定具體的屬性配置文件,優先級比較低。
SpringApplication.setDefaultProperties
例如:
SpringApplication application = new SpringApplication(Application.class); Map<String, Object> defaultMap = new HashMap<String, Object>(); defaultMap.put("name", "Isea-Blog"); //還可以是Properties對象 application.setDefaultProperties(defaultMap); application.run(args);
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
應用(使用)屬性
@Value(“${xxx}”)
這種方式是最簡單的,通過@Value
注解可以將屬性值注入進來。
@ConfigurationProperties
Spring Boot 可以方便的將屬性注入到一個配置對象中。例如:
my.name=Isea533 my.port=8080 my.servers[0]=dev.bar.com my.servers[1]=foo.bar.com
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
對應對象:
@ConfigurationProperties(prefix="my") public class Config { private String name; private Integer port; private List<String> servers = new ArrayList<String>(); public String geName(){ return this.name; } public Integer gePort(){ return this.port; } public List<String> getServers() { return this.servers; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Spring Boot 會自動將prefix="my"
前綴為my
的屬性注入進來。
Spring Boot 會自動轉換類型,當使用List
的時候需要注意在配置中對List
進行初始化!
Spring Boot 還支持嵌套屬性注入,例如:
name=isea533
jdbc.username=root
jdbc.password=root
...
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
對應的配置類:
@ConfigurationProperties public class Config { private String name; private Jdbc jdbc; class Jdbc { private String username; private String password; //getter... } public Integer gePort(){ return this.port; } public Jdbc getJdbc() { return this.jdbc; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
jdbc
開頭的屬性都會注入到Jdbc
對象中。
在@Bean方法上使用@ConfigurationProperties
例如:
@ConfigurationProperties(prefix = "foo") @Bean public FooComponent fooComponent() { ... }
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
Spring Boot 會將foo
開頭的屬性按照名字匹配注入到FooComponent
對象中。
屬性占位符
例如:
app.name=MyApp app.description=${app.name} is a Spring Boot application
- 1
- 2
- 1
- 2
可以在配置文件中引用前面配置過的屬性(優先級前面配置過的這里都能用)。
通過如${app.name:默認名稱}
方法還可以設置默認值,當找不到引用的屬性時,會使用默認的屬性。
由於${}
方式會被Maven處理。如果你pom繼承的spring-boot-starter-parent
,Spring Boot 已經將maven-resources-plugins
默認的${}
方式改為了@ @
方式,例如@name@
。
如果你是引入的Spring Boot,你可以修改使用其他的分隔符
通過屬性占位符還能縮短命令參數
例如修改web默認端口需要使用--server.port=9090
方式,如果在配置中寫上:
server.port=${port:8080}
- 1
- 1
那么就可以使用更短的--port=9090
,當不提供該參數的時候使用默認值8080
。
屬性名匹配規則
例如有如下配置對象:
@Component @ConfigurationProperties(prefix="person") public class ConnectionSettings { private String firstName; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
firstName
可以使用的屬性名如下:
person.firstName
,標准的駝峰式命名person.first-name
,虛線(-
)分割方式,推薦在.properties
和.yml
配置文件中使用PERSON_FIRST_NAME
,大寫下划線形式,建議在系統環境變量中使用
屬性驗證
可以使用JSR-303
注解進行驗證,例如:
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { @NotNull private InetAddress remoteAddress; // ... getters and setters }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
最后
以上是Spring Boot 屬性配置和使用的內容,有些不全面的地方或者讀者有更多疑問,可以查看Spring Boot完整文檔 或 Externalized Configuration。
關於Spring Boot更多的內容可以繼續關注本博客。