@ConfigurationProperties配置及注意事项

1. 加载方式

@ConfigurationProperties(prefix = "another") 
@Bean 
public AnotherComponent anotherComponent() {     ... }
@ConfigurationProperties("acme") 
public class AcmeProperties {}

2 .Binding Maps


When binding to Map properties, if the key contains anything other than lowercase alpha-numeric characters or -, you need to use the bracket notation so that the original value is preserved. If the key is not surrounded by [], any characters that are not alpha-numeric or - are removed. For example, consider binding the following properties to a Map:

@ConfigurationProperties("acme") 
public class AcmeProperties {
    private final Map<String, MyPojo> map = new HashMap<>();
    public Map<String, MyPojo> getMap() {         
        return this.map;
    }
}

 

3. List

 

4.注意事项

 PORT binds to port
 To convert a property name in the canonical-form to an environment variable name you can follow these rules:
 

• Replace dots (.) with underscores (_).

• Remove any dashes (-). 

• Convert to uppercase

For example, the configuration property spring.main.log-startup-info would be an environment variable named SPRING_MAIN_LOGSTARTUPINFO.

 @Value("{demo.item-price}") will pick up demo.item-price and demo.itemPrice forms from the application.properties file, as well as DEMO_ITEMPRICE from the system environment. If you used @Value("{demo.itemPrice}") instead, demo.item-price and DEMO_ITEMPRICE would not be considered.
 

5.实际应用

@Configuration
@ConfigurationProperties(prefix = "weixinapi")
@Data
public class WxProperties {

	private String baseUrl;
	private String secret;
	private String appId;
	private String grantType;

}
@Autowired
private WxProperties wxProperties;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值