- Mastering Spring Boot 2.0
- Dinesh Rajput
- 215字
- 2021-06-25 21:29:15
Externally configuring application properties
Spring Boot allows you to create your own customized configuration of application properties with beans. You can register these beans as properties with Spring Boot by using the @ConfigurationProperties annotation, and after this, you can set these properties by using either the application.properties or application.yml file.
Spring Boot provides alternative ways of working with properties that allow you to strongly type safe beans and validate the configuration of your application. Let's see the use of the @ConfigurationProperties annotation for a dedicated container bean:
- This @ConfigurationProperties annotation will hold the externalized properties
- It avoids repeating the prefix
- Data members automatically set from corresponding properties
Let's see the following example:
@Component @ConfigurationProperties(prefix="accounts.client") public class ConnectionSettings { private String host; private int port; private String logdir; private int timeout; ... // getters/setters ... }
This POJO defines the following properties in the application.properties file:
accounts.client.host=192.168.10.21 accounts.client.port=8181 accounts.client.logdir=/logs accounts.client.timeout=4000
You could set these properties as environment variables, or you could specify these properties as command-line arguments, or you can add these in any of the other places where configuration properties can be set.
Don't forget to add @EnableConfigurationProperties in one of your Spring configuration classes because the @ConfigurationProperties annotation won't work unless you have enabled it by adding the @EnableConfigurationProperties annotation.
- Word/Excel/PowerPoint 2013三合一高效辦公超級手冊
- 電腦辦公(Windows 7 + Office 2013)入門與提高(超值版)
- 從原始數(shù)據(jù)到分析報告:Excel數(shù)據(jù)透視表高效達(dá)人養(yǎng)成記
- Word排版技術(shù)大全(第2版)
- Getting Started with Simulink
- MATLAB R2020a完全自學(xué)一本通
- 和秋葉一起學(xué):秒懂WPS文字處理
- Excel 2013從新手到高手(超值版)
- 中文版AutoCAD 2016園林設(shè)計從入門到精通
- Excel行政文秘應(yīng)用之道
- Office2010從入門到精通(視頻講解+圖解+技巧)
- 隨身查: 一小時搞定你想要的PPT
- Word/Excel 2010高效辦公:文秘與行政辦公
- PowerPoint 2016從入門到精通
- Excel函數(shù)與公式速查手冊(第2版)