SpringBoot配置文件映射到JavaBean

SpringBoot配置文件映射到JavaBean

默认的配置文件application.properties

# 自定义实体类,映射
dog.name=小狗
dog.age=12

自定义配置文件person.properties

 

# 自定义实体类,映射
# ${random.uuid} 随机的uuid
person.lastName=zhangsan${random.uuid}
# ${random.int(20)} 20以内的随机整数
person.age=${random.int(20)}
person.boss=false
person.birth=2017/12/12
# ${random.int[1024,65536]} 范围之内的随机整数
person.maps.k1=${random.int[1024,65536]}
person.maps.k2=${random.int[1024,65536]}
# ${person.lastName:name} 获取之前配置的值,没有则使用默认值,下例${person.hello:hello}=hello ${person.lastName:name}=zhangsanuuid
person.lists=cat,${person.hello:hello}_dog,${person.lastName:name}_pig

 

Dog实体类

 

 1 package com.vegeta.spring_boot.entities;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class Dog {
 8     @Value("${dog.name}")
 9     private String name;
10     @Value("${dog.age}")
11     private Integer age;
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public Integer getAge() {
22         return age;
23     }
24 
25     public void setAge(Integer age) {
26         this.age = age;
27     }
28 
29     @Override
30     public String toString() {
31         return "Dog{" +
32                 "name='" + name + '\'' +
33                 ", age=" + age +
34                 '}';
35     }
36 }

 

Person实体类

 

package com.vegeta.spring_boot.entities;

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

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @PropertySource("classpath:person.properties")
 * 指定加载配置文件的位置
 *
 * @ConfigurationProperties
 * 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
 */
@Component
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,String> maps;
    private List<String> lists;
    @Resource
    private 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 getBoss() {
        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, String> getMaps() {
        return maps;
    }

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

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

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

    public Dog getDog() {
        return dog;
    }

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

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

 

测试类

 

 1 package com.vegeta.spring_boot.controller;
 2 
 3 import com.vegeta.spring_boot.entities.Person;
 4 import org.junit.Test;
 5 import org.junit.runner.RunWith;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import javax.annotation.Resource;
10 
11 @RunWith(SpringRunner.class)
12 @SpringBootTest
13 public class EmployeeControllerTest {
14     @Resource
15     private Person person;
16 
17     @Test
18     public void test() {
19         System.out.println(person);
20     }
21 
22 }

 

打印结果

Person{lastName='zhangsan0b7728ee-86e1-4f6f-8302-89da57abc6a1', age=3, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k2=2546, k1=63007}, lists=[cat, hello_dog, zhangsan561e37e8-28f2-4183-9fcd-f2216c5edd07_pig], dog=Dog{name='小狗', age=12}}

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18486

发表评论