@PropertySource 代码解释

@PropertySource:加载指定的配置文件

 Person.java

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;

/**
 * @Classname Person
 * @date 2019/8/24 12:15
 */
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;
    private Boolean stage;
    private Date birth;
    private Map maps;
    private List lists;
    private Child child;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    public Boolean getStage() {
        return stage;
    }

    public void setStage(Boolean stage) {
        this.stage = stage;
    }

    public Date getBirth() {
        return birth;
    }

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

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

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

    public Child getChild() {
        return child;
    }

    public void setChild(Child child) {
        this.child = child;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", stage=" + stage +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", child=" + child +
                '}';
    }
}

person.properties

person.name=小明
person.age=22
person.birth=2019/01/01
person.stage=true
person.maps.name=jack
person.maps.age=16
person.lists=a,b
person.child.name=露西
person.child.age=11

测试:

import com.atguigu.springboot.pojo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldApplicationTests {

	@Autowired
	private Person person;

	@Test
	public void contextLoads() {
		System.out.println(person.toString());
	}


}

 

结果:

Person{name='小明', age=22, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b], child=Child{name='露西', age=11}}

你可能感兴趣的:(SpringBoot)