forked from ITDragonBlog/daydayup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f53a3e
commit 026f9f6
Showing
8 changed files
with
612 additions
and
112 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...Boot/spring-boot-yml/src/main/java/com/itdragon/entity/ConfigurationPropertiesEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.itdragon.entity; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
import javax.validation.constraints.Email; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* ConfigurationProperties 注解语法类 | ||
* 第一步:导入依赖 spring-boot-configuration-processor; | ||
* 第二步:把ConfigurationProperties注解修饰的类添加到Spring的IOC容器中; | ||
* 第三步:设置prefix属性,指定需要注入属性的前缀; | ||
* 第四步:添加数据校验注解,开启数据校验; | ||
* | ||
* 注意点: | ||
* 一、nickName和createdDate在yml配置文件中,对应参数分别是中划线和下划线,用于测试其对属性名匹配的松散性 | ||
* 二、email和iphone 测试其支持JSR303数据校验 | ||
* 三、abilities 测试其支持复杂的数据结构 | ||
* | ||
* 若想运行成功,需要注释33行 | ||
*/ | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "itdragon") | ||
@Validated | ||
public class ConfigurationPropertiesEntity { | ||
|
||
private String nickName; // 解析成功,支持松散匹配属性 | ||
private String email; | ||
// @Email // 解析失败,数据校验成功:BindValidationException: Binding validation errors on itdragon | ||
private String iphone; | ||
private List<String> abilities; | ||
private Date createdDate; // 解析成功,支持松散匹配属性 | ||
|
||
// @ConfigurationProperties("#{(1+2-3)/4*5}") | ||
private String operator; // 语法报错,不支持SpEL表达式:not applicable to field | ||
|
||
public String getNickName() { | ||
return nickName; | ||
} | ||
|
||
public void setNickName(String nickName) { | ||
this.nickName = nickName; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getIphone() { | ||
return iphone; | ||
} | ||
|
||
public void setIphone(String iphone) { | ||
this.iphone = iphone; | ||
} | ||
|
||
public List<String> getAbilities() { | ||
return abilities; | ||
} | ||
|
||
public void setAbilities(List<String> abilities) { | ||
this.abilities = abilities; | ||
} | ||
|
||
public Date getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(Date createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ConfigurationPropertiesEntity{" + | ||
"nickName='" + nickName + '\'' + | ||
", email='" + email + '\'' + | ||
", iphone='" + iphone + '\'' + | ||
", abilities=" + abilities + | ||
", createdDate=" + createdDate + | ||
'}'; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
SpringBoot/spring-boot-yml/src/main/java/com/itdragon/entity/RandomEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.itdragon.entity; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 随机数和占位符语法类 | ||
*/ | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "ran") | ||
public class RandomEntity { | ||
|
||
private String ranValue; // 随机生成一个字符串 | ||
private Integer ranInt; // 随机生成一个整数 | ||
private Long ranLong; // 随机生成一个长整数 | ||
private Integer ranIntNum; // 在指定范围内随机生成一个整数 | ||
private Integer ranIntRange;// 在指定区间内随机生成一个整数 | ||
private String ranPlaceholder;// 占位符 | ||
|
||
public String getRanValue() { | ||
return ranValue; | ||
} | ||
|
||
public void setRanValue(String ranValue) { | ||
this.ranValue = ranValue; | ||
} | ||
|
||
public Integer getRanInt() { | ||
return ranInt; | ||
} | ||
|
||
public void setRanInt(Integer ranInt) { | ||
this.ranInt = ranInt; | ||
} | ||
|
||
public Long getRanLong() { | ||
return ranLong; | ||
} | ||
|
||
public void setRanLong(Long ranLong) { | ||
this.ranLong = ranLong; | ||
} | ||
|
||
public Integer getRanIntNum() { | ||
return ranIntNum; | ||
} | ||
|
||
public void setRanIntNum(Integer ranIntNum) { | ||
this.ranIntNum = ranIntNum; | ||
} | ||
|
||
public Integer getRanIntRange() { | ||
return ranIntRange; | ||
} | ||
|
||
public void setRanIntRange(Integer ranIntRange) { | ||
this.ranIntRange = ranIntRange; | ||
} | ||
|
||
public String getRanPlaceholder() { | ||
return ranPlaceholder; | ||
} | ||
|
||
public void setRanPlaceholder(String ranPlaceholder) { | ||
this.ranPlaceholder = ranPlaceholder; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RandomEntity{" + | ||
"ranValue='" + ranValue + '\'' + | ||
", ranInt=" + ranInt + | ||
", ranLong=" + ranLong + | ||
", ranIntNum=" + ranIntNum + | ||
", ranIntRange=" + ranIntRange + | ||
", ranPlaceholder='" + ranPlaceholder + '\'' + | ||
'}'; | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
SpringBoot/spring-boot-yml/src/main/java/com/itdragon/entity/ValueEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package com.itdragon.entity; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
import javax.validation.constraints.Email; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
/** | ||
* Value 注解语法类 | ||
* 第一步:在属性上添加注解Value注入参数 | ||
* 第二步:把Value注解修饰的类添加到Spring的IOC容器中; | ||
* 第三步:添加数据校验注解,检查是否支持数据校验; | ||
* | ||
* 注意点: | ||
* 一、nickName和createdDate在yml配置文件中,对应参数分别是中划线和下划线,用于测试其对属性名匹配的松散性 | ||
* 二、email和iphone 测试其支持JSR303数据校验 | ||
* 三、abilities 测试其支持复杂的数据结构 | ||
* | ||
* 结论: | ||
* 一、createDate取值必须和yml配置文件中的参数保持一致, | ||
* 二、既是在iphone上添加邮箱验证注解依然可以通过测试, | ||
* 三、不支持复杂的数据结构,提示错误和第一条相同:IllegalArgumentException: Could not resolve placeholder 'itdragon.abilities' in value "${itdragon.abilities}" | ||
* | ||
* 若想运行成功,需要注释42行;修改44行,将ceatredDate修改为ceatred_date | ||
*/ | ||
|
||
@Component | ||
@Validated | ||
public class ValueEntity { | ||
|
||
@Value("${itdragon.nick-name}") | ||
private String nickName; | ||
@Value("${itdragon.email}") | ||
private String email; | ||
@Value("${itdragon.iphone}") // 解析成功,并不支持数据校验 | ||
private String iphone; | ||
// @Value("${itdragon.abilities}") // 解析错误,并不支持复杂的数据结构 | ||
private List<String> abilities; | ||
// @Value("${itdragon.ceatredDate}") // 解析错误,并不支持松散匹配属性,必须严格一致 | ||
private Date createdDate; | ||
|
||
// Value注解的强大一面:支持SpEL表达式 | ||
@Value("#{(1+2-3)/4*5}") // 算术运算 | ||
private String operator; | ||
@Value("#{1>2 || 2 <= 3}") // 关系运算 | ||
private Boolean comparison; | ||
@Value("#{systemProperties['java.version']}") // 系统配置:os.name | ||
private String systemProperties; | ||
@Value("#{T(java.lang.Math).abs(-18)}") // 表达式 | ||
private String mapExpression; | ||
|
||
public String getNickName() { | ||
return nickName; | ||
} | ||
|
||
public void setNickName(String nickName) { | ||
this.nickName = nickName; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getIphone() { | ||
return iphone; | ||
} | ||
|
||
public void setIphone(String iphone) { | ||
this.iphone = iphone; | ||
} | ||
|
||
public List<String> getAbilities() { | ||
return abilities; | ||
} | ||
|
||
public void setAbilities(List<String> abilities) { | ||
this.abilities = abilities; | ||
} | ||
|
||
public Date getCreatedDate() { | ||
return createdDate; | ||
} | ||
|
||
public void setCreatedDate(Date createdDate) { | ||
this.createdDate = createdDate; | ||
} | ||
|
||
public String getOperator() { | ||
return operator; | ||
} | ||
|
||
public void setOperator(String operator) { | ||
this.operator = operator; | ||
} | ||
|
||
public Boolean getComparison() { | ||
return comparison; | ||
} | ||
|
||
public void setComparison(Boolean comparison) { | ||
this.comparison = comparison; | ||
} | ||
|
||
public String getSystemProperties() { | ||
return systemProperties; | ||
} | ||
|
||
public void setSystemProperties(String systemProperties) { | ||
this.systemProperties = systemProperties; | ||
} | ||
|
||
public String getMapExpression() { | ||
return mapExpression; | ||
} | ||
|
||
public void setMapExpression(String mapExpression) { | ||
this.mapExpression = mapExpression; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ValueEntity{" + | ||
"nickName='" + nickName + '\'' + | ||
", email='" + email + '\'' + | ||
", iphone='" + iphone + '\'' + | ||
", abilities=" + abilities + | ||
", createdDate=" + createdDate + | ||
", operator='" + operator + '\'' + | ||
", comparison=" + comparison + | ||
", systemProperties='" + systemProperties + '\'' + | ||
", mapExpression='" + mapExpression + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,21 @@ yaml: | |
- name: ITDragonBlog | ||
salary: 18888.88 | ||
|
||
--- | ||
itdragon: | ||
nick-name: ITDragonBlog | ||
email: [email protected] | ||
iphone: 1234567890 | ||
abilities: [java, sql, html] | ||
created_date: 2018/03/31 15:27:30 | ||
|
||
ran: # 这里的prefix不能是random, | ||
ran-value: ${random.value} | ||
ran-int: ${random.int} | ||
ran-long: ${random.long} | ||
ran-int-num: ${random.int(10)} | ||
ran-int-range: ${random.int[10,20]} | ||
ran-placeholder: placeholder_${ran.ran-value:此处不能有空格,且key为完整路径} | ||
|
||
userinfo: | ||
account: itdragon | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters