SpringBoot——读取配置文件@Value和@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 java.util.Map;

/**
 * 将配置文件中的配置映射到这个组件中
 *
 * @ConfigurationProperties 告诉springboot将本类中的所有属性和配置文件中的相关配置进行绑定:
 * prefix:配置文件中的哪个属性下的所有属性进行一一绑定
 * 只有该组件是容器中的组件才可以发挥作用(提供@ConfigurationProperties功能)
 * 可以在test中进行单元测试
 */
@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

 

 

 

二者比较

 

补充读取.properties文件乱码问题

 

posted @ 2019-10-02 17:24  习惯沉淀  阅读(3740)  评论(0编辑  收藏  举报