springboot reads the configuration file @Value and @Configuration

1.@Configuration

Package com.xgcd.springboot.bean; 

Import org.springframework.boot.context.properties.ConfigurationProperties;
 Import org.springframework.context.annotation.PropertySource;
 Import org.springframework.stereotype.Component; 

Import java.util.Date;
 Import java.util.List;
 Import a java.util.Map; 

/ ** 
 * mapping configuration file configured to the component 
 * 
 * @ConfigurationProperties springboot to tell all of the properties and the profile of this class relevant configuration tie fixed: 
 * prefix: all attributes under which property in the configuration file one by one binding 
 * this component is only a container of components can play a role (providing @ConfigurationProperties function) 
 * can be unit tested in the test 
 * / 
@ the Component 
@ConfigurationProperties (prefix= "person")
@PropertySource(value = {"classpath:application.properties"}, encoding = "utf-8")
public class Person {
    private String lastName;
    private Integer age;
    private boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

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

    public boolean isBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
}

 

 

2.@Value

 

 

 

Comparison of the two

 

Supplementary reading .properties file garbage problem

 

Guess you like

Origin www.cnblogs.com/yadongliang/p/11617832.html