SpringBoot配置文件占位符

SpringBoot配置文件占位符_第1张图片 

application.properties

${testDefault:defaultValue}:默认值--》defaultValue

#person
person.name=詹金斯${random.uuid}
person.age=${random.int}
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.name}--${testDefault:defaultValue}--testName
person.child.age=11

Person.java

package com.atguigu.springboot.pojo;

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
 */
@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 +
                '}';
    }
}
 
  

测试:

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='詹金斯cd917e06-c7a8-4a47-beb0-05eaa7c4df9b', age=-1936523024, 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='詹金斯0e902f4e-c659-4c01-94bc-5a75e6eaa5e4--defaultValue--testName', age=11}}

你可能感兴趣的:(SpringBoot)