@Value difference @ConfigurationProperties annotations and annotations

Are read configuration file attributes

@ 1. Configuration Properties (prefix = "Person") reading a plurality of attributes

 

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    String LastName;
    int age;
    boolean boss;
    Date birth;
    Map<String, String> maps;
    List<String> list;
    Dog dog;

 

prefix = "person" read prefix Person . attribute value 
attribute value corresponding to the file yaml:
person:
  LastName: 飞
  age: 12
  boss: true
  birth: 2019/6/15
  maps: {key1:value1,key2:value2}
  list: [1,2,3,4,5]
  dog:
    name: 旺财
    age: ${random.int[20]}
 

@ 1. value read individual properties

 

@Component
public class Person1 {
    @Value("${person.last-name}")
    String LastName;
    //@Value("${person.age}")
   @Value("#{12*2}")
int age; @Value("${person.boss}") boolean boss; @Value("${person.birth}") Date birth; Map<String, String> maps; @Value("${person.list}") List<String> list; Dog dog;

 

Note: maps and the dog can not be used @value annotation, @ Value does not support complex types

     

 

 application.properties file configuration

person.last-name=飞
person.age=12
person.boss=true
person.birth=2019/6/15
person.maps.key1=value1
person.maps.key2=value2
person.list=dog,cat
person.dog.name=${person.last-name}_你好
person.dog.age=${random.int(14)}

 

 Both get the value of the comparison (value can be obtained from the properties and yaml)

  @ConfigurationProperties @value
Features Batch injection property profile A designated
Loosely bound (loose syntax) stand by not support
GAME not support Support (calculated, the values ​​shown above age)
JSR303 data check Support (email address Verification) not support
Complex type package stand by not support

 

 

 

 

 

 

Obviously, the former support deregulation set features more powerful, it is recommended to use @ConfigurationProperties to read custom properties in the actual development.

 

Guess you like

Origin www.cnblogs.com/lh-cml/p/11026896.html