跟杨春娟学SpringBoot笔记:如何properties文件中参数之间相互调用和通过properties文件来设置随机值?
完成:第一遍
1.如何properties文件中参数之间相互调用和通过properties文件来设置随机值?
步骤一:在config.properties中设置参数间的引用
config.properties
myboot.aws.key=23o48uworow3uro99w3ur3wurow3u
myboot.aws.region=cn-north-1
myboot.aws.bucketName=cherry
myboot.aws.show=${myboot.aws.region}--${myboot.aws.bucketName}
#random
myboot.random.secret=${random.value}
//分别设置int、long、uuid等类型的随机数
myboot.random.number=${random.int}
myboot.random.bigNumber=${random.long}
myboot.random.uuid=${random.uuid}
myboot.random.ten=${random.int(10)}
myboot.random.range=${random.int[1024,65536]}
步骤二:修改AWSConfigBean
AWSConfigBean
package com.springboot.demo.SpringBootDemoProject.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="myboot.aws")
public class AWSConfigBean {
private String key;
private String region;
private String bucketName;
private String show;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getShow() {
return show;
}
public void setShow(String show) {
this.show = show;
}
}
步骤三:新建RandomConfigBean.java
RandomConfigBean.java
package com.springboot.demo.SpringBootDemoProject.configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="myboot.random")
public class RandomConfigBean {
private String secret;
private int number;
private long bigNumber;
private String uuid;
private int ten;
private int range;
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public long getBigNumber() {
return bigNumber;
}
public void setBigNumber(long bigNumber) {
this.bigNumber = bigNumber;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getTen() {
return ten;
}
public void setTen(int ten) {
this.ten = ten;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
}
步骤三:修改config.html
config.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This is a Configuration Test page</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
This is Configuration Test page!
<p th:text="${awsKey}"></p>
<p th:text="${awsRegion}"></p>
<p th:text="${awsBucketName}"></p>
<p th:text="${configBean.key}"></p>
<p th:text="${configBean.region}"></p>
<p th:text="${configBean.bucketName}"></p>
<p th:text="${'this is show'+configBean.show}"></p>
<p>Following is random value</p>
<p th:text="${'secret:'+randomConfigBean.secret}"></p>
<p th:text="${'number:'+randomConfigBean.number}"></p>
<p th:text="${'bigNumber:'+randomConfigBean.bigNumber}"></p>
<p th:text="${'uuid:'+randomConfigBean.uuid}"></p>
<p th:text="${'ten'+randomConfigBean.ten}"></p>
<p th:text="${'range'+randomConfigBean.range}"></p>
</body>
</html>
步骤四:修改config.html
model.addAttribute(“randomConfigBean”, randomConfigBean);
package com.springboot.demo.SpringBootDemoProject.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("c")
public class ConfigurationPageController {
@Value("${myboot.aws.key}")
private String awsKey;
@Value("${myboot.aws.region}")
private String awsRegion;
@Value("${myboot.aws.bucketName}")
private String awsBucketName;
@Autowired
private AWSConfigBean aWSConfigBean;
@Autowired
private RandomConfigBean randomConfigBean;
@RequestMapping("value")
public String show(ModelMap model) {
model.addAttribute("awsKey", awsKey);
model.addAttribute("awsRegion", awsRegion);
model.addAttribute("awsBucketName", awsBucketName);
model.addAttribute("configBean", aWSConfigBean);
model.addAttribute("randomConfigBean", randomConfigBean);
return "config";
}
}