properties随机数与配置文件占位符

140 篇文章 4 订阅

目录

 

理论

例子


理论

RandomValuePropertySource:配置文件中可以使用随机数

${random.value}、${random.int}、${random.long}、${random.int(10)}、${random.int[1024, 65534]}

 

属性配置占位符

people.name="你妹"
people.alias=${people.name}"呵呵"

 

例子

程序结构如下:

运行截图如下:

源码如下:

People.java

package com.setactive.demo.bean;

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

@Component
@ConfigurationProperties(prefix = "people")
public class People {

    private String name;
    private String aliay;
    private int age;
    private int weight;
    private int height;

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", aliay='" + aliay + '\'' +
                ", age=" + age +
                ", weight=" + weight +
                ", height=" + height +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getAliay() {
        return aliay;
    }

    public void setAliay(String aliay) {
        this.aliay = aliay;
    }

    public int getAge() {
        return age;
    }

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

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

DemoApplication.java

package com.setactive.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

application.properties

people.name="你妹"${random.uuid}
people.aliay="日了狗了"${random.uuid}
people.height=${random.int}
people.weight=${random.int}

DemoApplicationTests.java

package com.setactive.demo;

import com.setactive.demo.bean.People;
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 DemoApplicationTests {

    @Autowired
    People people;

    @Test
    public void contextLoads() {

        System.out.println(people);
    }

}

修改application.properties文件

#people.name="你妹"${random.uuid}
people.aliay=${people.name:呵呵}呵呵哒
people.height=${random.int}
people.weight=${random.int}

运行截图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值