SpringBoot系列之配置文件占位符使用

SpringBoot系列之配置文件占位符使用

Springboot占位符支持的有随机数和配置的值等等,本博客主要介绍的是随机数和获取属性配置值的简单用法

  • 随机数获取

支持的写法有:

${random.value}、${random.int}、${random.long}、${random.uuid}
${random.int(10)}、${random.int(1024,65536)} .etc
  • 获取属性配置的值
user.userName= root(${user.address.tel})
user.address.tel= 15899988899

ok,写个例子实践一下

user.properties

user.userName= root(${user.address.tel})
user.isAdmin= true
user.regTime= 2019/11/01
user.isOnline= 1
user.maps.k1=${random.int}
user.maps.k2=v2
user.lists=${random.uuid},${random.value}
user.address.tel= 15899988899
user.address.name=上海浦东区

User类


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;

@Component//将组件添加到Spring容器
@PropertySource(value = "classpath:user.properties",encoding = "utf-8")
@ConfigurationProperties(prefix = "user")//属性进行映射
public class User {

    private String userName;
    private boolean isAdmin;
    private Date regTime;
    private Long isOnline;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Address address;

    @Override
    public String toString() {
        return "User{" +
                       "userName='" + userName + '\'' +
                       ", isAdmin=" + isAdmin +
                       ", regTime=" + regTime +
                       ", isOnline=" + isOnline +
                       ", maps=" + maps +
                       ", lists=" + lists +
                       ", address=" + address +
                       '}';
    }

    public String getUserName() {
        return userName;
    }

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

    public boolean isAdmin() {
        return isAdmin;
    }

    public void setAdmin(boolean admin) {
        isAdmin = admin;
    }

    public Date getRegTime() {
        return regTime;
    }

    public void setRegTime(Date regTime) {
        this.regTime = regTime;
    }

    public Long getIsOnline() {
        return isOnline;
    }

    public void setIsOnline(Long isOnline) {
        this.isOnline = isOnline;
    }

    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 Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Address类::

package com.example.springboot.properties.bean;

/**
 * <pre>
 *
 * </pre>
 *
 * @author nicky
 * <pre>
 * 修改记录
 *    修改后版本:     修改人:  修改日期: 2019年11月03日  修改内容:
 * </pre>
 */
public class Address {
    private String tel;
    private String name;

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

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getName() {
        return name;
    }

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

写个junit测试类获取

package com.example.springboot.properties;

import com.example.springboot.properties.bean.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

@SpringBootTest
class SpringbootPropertiesConfigApplicationTests {

    @Autowired
    User user;

    @Test
    public void testConfigurationProperties(){
        System.out.println(user);
    }

}
User{userName='root(15899988899)', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k2=v2, k1=366784341}, lists=[2c9f4a24-23d9-4507-b446-f8e92c82608f, 0c61b1f7321714f374ba16f88c1bb587], address=Address{tel='15899988899', name='上海浦东区'}}

猜你喜欢

转载自www.cnblogs.com/mzq123/p/11830435.html