侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringBoot(5)之配置与属性的映射注解

zze
zze
2018-01-16 / 0 评论 / 0 点赞 / 643 阅读 / 8468 字

现有如下 .yml.properties 全局配置文件:

# application.yml
user:
  user-name: jack
  age: 18
  birthday: 1997/2/3
  boss: true
  hobbys: [eat,sleep,drink]
  map: {k1: v1,k2: v2}
  dog: {name: jumi,color: yellow}
# application.properties
user.user-name=jack
user.age=18
user.birthday=1997/2/3
user.boss=true
user.hobbys:eat,sleep,drink
user.map.k1=v1
user.map.k2=v2
user.dog.name=jumi
user.dog.color=yellow

@ConfiguartionProperties:从全局配置文件中绑定值到标注类的属性

1、编写测试的 POJO:

// com.zze.helloworld.pojo.Dog
public class Dog {
    private String name;

    private String color;

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}
// com.zze.helloworld.pojo.User
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

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

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 *
 * @ConfigurationProperties:告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定
 * prefix = "user":配置文件中哪个下面的属性进行映射
 * <p>
 * 只有这个组件是容器中的组件,才能使用容器提供的 @ConfigurationProperties 注解
 */
@Component
@ConfigurationProperties(prefix = "user")
public class User {
    private String userName;

    private Integer age;

    private Date birthday;

    private Boolean isBoss;

    private List<String> hobbys;

    private Map<String, String> map;

    private Dog dog;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Boolean getBoss() {
        return isBoss;
    }

    public void setBoss(Boolean boss) {
        isBoss = boss;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Dog getDog() {
        return dog;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + userName + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", isBoss=" + isBoss +
                ", hobbys=" + hobbys +
                ", map=" + map +
                ", dog=" + dog +
                '}';
    }
}

2、测试:

import com.zze.helloworld.pojo.User;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
    @Autowired
    public User user;

    @Test
    public void test() {
        System.out.println(user);
        /*
        User{name='jack', age=18, birthday=Mon Feb 03 00:00:00 CST 1997, isBoss=true, hobbys=[eat, sleep, drink], map={k1=v1, k2=v2}, dog=Dog{name='jumi', color='yellow'}}
         */
    }

}

@Value:从全局配置文件绑定值到标注属性

1、编写测试 POJO:

// com.zze.helloworld.pojo.User
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class User {
    @Value("${user.user-name}")
    private String userName;
    @Value("${user.age}")
    private Integer age;
    @Value("${user.birthday}")
    private Date birthday;
    @Value("${user.boss}")
    private Boolean isBoss;

    private List<String> hobbys;

    private Map<String, String> map;

    private Dog dog;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Boolean getBoss() {
        return isBoss;
    }

    public void setBoss(Boolean boss) {
        isBoss = boss;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Dog getDog() {
        return dog;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + userName + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", isBoss=" + isBoss +
                ", hobbys=" + hobbys +
                ", map=" + map +
                ", dog=" + dog +
                '}';
    }
}

2、测试:

import com.zze.helloworld.pojo.User;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloworldApplicationTests {
    @Autowired
    public User user;

    @Test
    public void test() {
        System.out.println(user);
        /*
        User{name='jack', age=18, birthday=Mon Feb 03 00:00:00 CST 1997, isBoss=true, hobbys=null, map=null, dog=null}
         */
    }
}

@ConfiguartionProperties 与 @Value 对比

 @ConfiguartionProperties@Value
功能批量给属性注入配置文件中值一个个指定
松散语法支持不支持
SPEL不支持支持
复杂类型封装支持不支持
松散语法:如配置文件中属性名为 `user-name` ,使用 `@ConfiguartionProperties` 注解能将其注入给 POJO 中 `userName` 属性,这就是支持松散语法。

在注入的同时还可以对将注入属性的值进行校验,遵循 JSR303 规范,如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "user")
@Validated  // 添加该注解表示要对注入的值进行校验
public class User {
    @Email // 该注解要求注入的值为 email 格式
    private String email;
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
0

评论区