3.1 Supplementary: SpringBoot property assignment, JSR303 verification, multi-environment configuration, and configuration file locations

.YMAL a placeholder, loosely bound, the entity class loading profile information is used to assign attributes

  • Placeholder:  $ {} random.int 
  • Loosely bound: Dog.java attributes named dogName, YMAL corresponding to dog-name
  • Entity class loading profile information:  @ConfigurationProperties (prefix = "Dog")  the configuration file and the age dog-name assigned to the entity classes and age dogName
person:
  name: qinjiang
  Age: $ random.int} {   # set value placeholder
  happy: false
  birth: 2000/01/01
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - girl
    - music
  dog:
    name-Dog : Cai
    age: ${random.int}

dog:
  name-Dog : A yellow # Name attribute loosely bound
  age: 5
@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
    private String dogName;
    private Integer age;

    public Dog() {
    }

    public Dog(String dogName, Integer age) {
        this.dogName = dogName;
        this.age = age;
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "dogName='" + dogName + '\'' +
                ", age=" + age +
                '}';
    }
}

Two .JSR 303 basic validation rules

Blank Check 

  1. @Null verify whether the object is null 
  2. @NotNull verify the object is not null, the search can not be the subject of a string of length 0 
  3. @NotBlank check constraint there is the string length is not Null Trim is greater than 0, only the string, and will remove the front and rear spaces. 
  4. @NotEmpty check constraint element is NULL or EMPTY.

Booelan inspection 

  1. Verify whether @AssertTrue Boolean object is true 
  2. Verify whether @AssertFalse Boolean object is false

Length check 

  1. @Size (min =, max =) authentication object (Array, Collection, Map, String) whether within a given length range in 
  2. @Length(min=, max=) Validates that the annotated string is between min and max included.

Date of inspection 

  1. @Past verification Date and Calendar object is before the current time, verification is established, then annotated element must be a date in the past 
  2. @Future verify whether the Date and Calendar objects after the current time, verification is established, then annotated element must be a future date 
  3. @Pattern verify String objects that meet the rules of regular expressions, annotated elements in line with the development of a regular expression, regexp: regular expression flags: the array designated Pattern.Flag, showing relevant options of regular expressions.

Check values 
recommended in Stirng, Integer type, not recommended for use in the int type, because the form is "" can not be converted when int, but can be converted into Stirng "", Integer is null 

  1. @Min verification Number and String object is equal to the specified value large 
  2. Verify whether @Max Number and String objects small equal to the specified value 
  3. @DecimalMax labeled value must not be greater than the maximum value specified constraints. This constraint parameter is a string defined by the maximum BigDecimal representation presence decimal precision 
  4. @DecimalMin labeled value must not be less than the minimum specified constraint. This constraint is a parameter defined by the minimum BigDecimal string representation presence decimal precision 
  5. Number and String @Digits verify the legality of the constitution 
  6. @Digits (integer =, fraction =) verify compliance with the string number is specified format, interger specified integer precision, fraction specify decimal precision. 
  7. @Range (min =, max =) element is specified to be within an appropriate range 
  8. @Range(min=10000,max=50000,message=”range.bean.wage”) 
  9. To the associated object @Valid recursively check, if the associated object is a set or array, wherein the elements are recursively checked and if it is a map, where the value of the checksum portion. (Verified whether recursion) 
  10. @CreditCardNumber credit card verification 
  11. Verify whether @Email mail address, if null, without verification, validation count. 
  12. @ScriptAssert (lang = script = alias =) 
  13. @URL(protocol=,host=, port=,regexp=, flags=)

Code shows:

  1. Data check for @Validated
  2. @Email set the property must be in email format
 1 @Component
 2 @ConfigurationProperties(prefix = "person")
 3 @Validated
 4 public class Person {
 5 
 6     @Email()
 7     private String name;
 8     private Integer age;
 9     private Boolean happy;
10     private Date birth;
11     private Map<String,Object> maps;
12     private List<Object> lists;
13     private Dog dog;
14 
15     public Person() {
16     }
17 
18     public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
19         this.name = name;
20         this.age = age;
21         this.happy = happy;
22         this.birth = birth;
23         this.maps = maps;
24         this.lists = lists;
25         this.dog = dog;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public Integer getAge() {
37         return age;
38     }
39 
40     public void setAge(Integer age) {
41         this.age = age;
42     }
43 
44     public Boolean getHappy() {
45         return happy;
46     }
47 
48     public void setHappy(Boolean happy) {
49         this.happy = happy;
50     }
51 
52     public Date getBirth() {
53         return birth;
54     }
55 
56     public void setBirth(Date birth) {
57         this.birth = birth;
58     }
59 
60     public Map<String, Object> getMaps() {
61         return maps;
62     }
63 
64     public void setMaps(Map<String, Object> maps) {
65         this.maps = maps;
66     }
67 
68     public List<Object> getLists() {
69         return lists;
70     }
71 
72     public void setLists(List<Object> lists) {
73         this.lists = lists;
74     }
75 
76     public Dog getDog() {
77         return dog;
78     }
79 
80     public void setDog(Dog dog) {
81         this.dog = dog;
82     }
83 
84     @Override
85     public String toString() {
86         return "Person{" +
87                 "name='" + name + '\'' +
88                 ", age=" + age +
89                 ", happy=" + happy +
90                 ", birth=" + birth +
91                 ", maps=" + maps +
92                 ", lists=" + lists +
93                 ", dog=" + dog +
94                 '}';
95     }
96 }

Source Location:

Configuration three multi-profile location and environment

1. The configuration of a position (position 4)

  1. file:./config/  ,项目目录下建立一个config文件夹,config文件夹下建立application.yaml
  2. file:./  ,项目目录下直接建立一个application.yaml
  3. classpath:/config/    ,resources下建立一个config文件夹,config文件夹下建立application.yaml
  4. classpath:/     ,resources下直接建立一个application.yaml

2. Configuration Priority: The above order from a reduced (  File: ./ config /  >  File: ./  >   CLASSPATH: / config /  >  CLASSPATH: /  )

3. Multiple configuration file to specify the configuration environment

  1. application.yaml: a configuration file used to specify which of activation (application-xxx.yaml, bound to activated xxx)

  2. application-test.yaml: Specifies the service port number 8082

  3. application-dev.yaml: Specifies the service port number 8081

4.YMAL simplify multi-file configuration

A file can be solved, separated by three horizontal lines

The depth understanding of automatic assembly

Essence:

1), SpringBoot will start automatically configured to load a lot of class

2), we see that we need to function there is no default automatic configuration class written among the SpringBoot;

3), we look at the automatic configuration class which components disposed in the end; (as long as we use components present therein, we do not need to manually configured)

4), when the automatic configuration class to add components, you will obtain certain properties from the properties of class to the vessel. We only need to specify the value of these properties in the configuration file;

xxxxAutoConfigurartion: automatic configuration class; component is added to the vessel

xxxxProperties: encapsulation configuration file related attributes;

application.yaml: for modifying the properties springboot

6. Review the configuration to take effect and which do not take effect

(1) application.yaml configuration  debug: true 

(2) take effect only need to add a starter in pom.xml to

Similar: spring-boot-starter-xxx

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12348283.html