06、Spring Boot,占位符
Spring Boot    2019-07-26 23:18:03    1154    0    0
acme   Spring Boot

    前面讲到了拆分配置文件,这里继续讲配置文件,主要是配置文件中的占位符。

    老样子,pom.xml先粘贴上。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cc.acme_me</groupId>
    <artifactId>springboot-prepare</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-prepare</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接着是实体类User

package cc.acme_me.springbootprepare.entity;


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

@PropertySource("classpath:user.properties")
@ConfigurationProperties(prefix = "user")
@Component
public class User {

    private String id;//ID

    private String userName;//用户名

    private int age;//年龄

    private String pet;//宠物名称

    private String role;//角色

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", userName='" + userName + '\'' +
                ", age=" + age +
                ", pet='" + pet + '\'' +
                ", role='" + role + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getPet() {
        return pet;
    }

    public void setPet(String pet) {
        this.pet = pet;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

前面都把配置文件拆分了,那么这里也拆分一下使用

#用户名
user.userName=user
#使用占位符 随机生成UUID
user.id=${random.uuid}
#随机生成年龄 小于18的整数
user.age=${random.int(18)}
#从配置中取值
user.pet=${user.userName}_pet
#从配置中取值 如果该值不存在,则默认为冒号后面的值
user.role=${system.role:access_user}

最后是测试类以及测试结果

package cc.acme_me.springbootprepare;

import cc.acme_me.springbootprepare.entity.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 SpringbootPrepareApplicationTests {

    @Autowired
    User user;
    @Test
    public void contextLoads() {
        System.out.println(user);
    }

}

Pre: 07、SpringBoot 内部、外部配置文件加载以及Profile配置

Next: 05、Spring Boot,拆分配置文件以及配置类注解

1154
Table of content