官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 吉林市| 阿巴嘎旗| 黎城县| 闸北区| 甘肃省| 台北县| 平潭县| 通渭县| 楚雄市| 谢通门县| 江油市| 安福县| 扬中市| 天镇县| 临潭县| 隆安县| 巴东县| 无棣县| 昌图县| 班戈县| 新郑市| 慈利县| 麻城市| 宾阳县| 汉阴县| 元朗区| 英山县| 灵璧县| 噶尔县| 铜陵市| 绩溪县| 翼城县| 静安区| 兴城市| 葫芦岛市| 铁岭市| 永昌县| 屯门区| 雷州市| 花垣县| 新沂市|