• 将配置文件中配置的每一个属性的值,映射到这个组件中
  • @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
  • prefix = "person":配置文件中哪个下面的所有属性进行一一映射
  • 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能

Person:

package com.atguigu.springboot.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Classname Person
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;
    private Boolean stage;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> 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<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 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 +
                '}';
    }
}

Child:

package com.atguigu.springboot.pojo;

/**
 * @Classname Child
 */
public class Child {
    private String name;
    private Integer age;

    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;
    }

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

导入配置文件处理器,以后编写配置就有提示  

                <!--生成自己的配置元数据文件
		包含一个Java注释处理器,在编译项目时调用该处理器-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

application.yml

person:
   name: typ
   age: 20
   stage: true
   birth: 2019/07/15
   maps: {name: jack, age: 12}
   lists:
    - dog
    - cat
   child:
    name: lucy
    age: 11



测试:

package com.atguigu.springboot;

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.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='typ', age=20, stage=true, birth=Mon Jul 15 00:00:00 CST 2019, maps={name=jack, age=12}, lists=[dog, cat], child=Child{name='lucy', age=11}}

 

application.properties

#person
person.name=詹金斯
person.age=20
person.birth=2019/01/01
person.stage=true
person.maps.name=jack
person.maps.age=16
person.lists=a,b,c,d,e,f
person.child.name=露西
person.child.age=11

结果(有乱码):

Person{name='ղ��˹', age=20, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b, c, d, e, f], child=Child{name='¶��', age=11}}

 File-->Settings

重新输入中文:

测试结果:

Person{name='詹金斯', age=20, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b, c, d, e, f], child=Child{name='露西', age=11}}

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐