@ConfigurationProperties和@Value
application.properties配置
person.name=admin
person.address=shanghai
person.email=123
person.map.k1=v1
person.map.k2=v2
先看一下@ConfigurationProperties的使用
/**
* 支援Validated校驗
*/
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
private String name;
private String address;
@Email
private String email;
private Map<String, Object> map;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
", map=" + map +
'}';
}
}
輸出:
Person{name='admin', address='shanghai', email='123@qq.com', map={k1=v1, k2=v2}}
再看一下@Value的使用
/**
* value註解不支援複雜型別,如map,list
*/
@Component
public class PersonValue {
@Value("${person.name}")
private String name;
@Value("${person.address}")
private String address;
//value註解支援SpEl表示式
@Value("#{20}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "PersonValue{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
}
輸出
PersonValue{name='admin', address='shanghai', age=20}
總結
@ConfigurationProperties | @Value | |
是否支援校驗 | 支援 | 不支援 |
是否支援複雜型別 | 支援 | 不支援 |
SpEl | 不支援 | 支援 |
相關文章
- 【springboot讀取配置檔案】@ConfigurationProperties、@PropertySource和@ValueSpring Boot
- SpringBoot通過@ConfigurationProperties註解和@Value讀取資原始檔中的值Spring Boot
- @EnableConfigurationProperties、@ConfigurationProperties
- ConfigurationProperties配置類
- Oracle分析函式-first_value()和last_value()Oracle函式AST
- Springboot註解@ConfigurationProperties報錯Spring Boot
- hashMap 中key和value互換HashMap
- textarea中的innerHtml,innerText和valueHTML
- @ConfigurationProperties實現自定義配置繫結
- 3.springboot-@Autowired和@Value工作原理Spring Boot
- Java交換map的key和value值Java
- golang multiple-value xxx in single-value contextGolangContext
- JQuery 獲取select被選中的value和textjQuery
- [20231103]sqlplus column new_value old_value.txtSQL
- [20230303]sqlplus column new_value old_value.txtSQL
- JavaScript select valueJavaScript
- Spring Boot通過@ConfigurationProperties訪問靜態資料 - reflectoringSpring Boot
- 利用 Spring Boot 中的 @ConfigurationProperties,優雅繫結配置引數Spring Boot
- 精盡Spring Boot原始碼分析 - @ConfigurationProperties 註解的實現Spring Boot原始碼
- WPF ProgressBar show value
- 7.94 FIRST_VALUE
- 7.92 FEATURE_VALUE
- 7.12 ANY_VALUE
- RESTOREkeyttlserialized-value[REPLACE]RESTTLSZed
- python: invalid value encountered in divide以及invalid value encountered in double_scalars報錯PythonIDE
- 為什麼Math.abs(Integr.MIN_VALUE)==Integer.MIN_VALUE
- get_attribute ('textContent') 和 get_attribute ('value') 有什麼區別
- Smart Value Help 總結
- solidity的msg.valueSolid
- Harmonic Value Description HDU - 5916
- tf.clip_by_value() 用法
- @Value失效的問題
- 如何拿到註解@ApiModelProperty(value = “單位名稱“, name = “orgName“)中的value值;API
- mybatis竟然報"Invalid value for getInt()"MyBatis
- attempt to index local ‘result‘ (a nil value)Index
- Transmit Value by Customized Annotation in AOP InterceptionMITZed
- Syntax error, unrecognized expression: li[value=]ErrorZedExpress
- Spring Boot 基礎: 使用 `@ConfigurationProperties` 實現自定義屬性的自動裝配Spring Boot