一,實體類配置:
package com.readyhuihui.testspring.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
/**
* @description 配置類
* @author liyonghui
* @date 2021/2/24 10:31
**/
@Validated
@Configuration
@EnableConfigurationProperties(Config.class)
@ConfigurationProperties(prefix = "config")
public class Config {
/**
* 媒體雲環境媒體雲租戶標識
*/
@NotNull(message = "配置文件配置必須要配置[config.to-company-id]屬性")
private String toCompanyId;
/**
* 媒體雲環境媒體雲用戶標識
*/
@NotNull(message = "配置文件配置必須要配置[config.to-user-id]屬性")
private String toUserId;
/**
* 媒體雲環境媒體雲接口地址
*/
@NotNull(message = "配置文件配置必須要配置[config.to-interface-url]屬性")
private String toInterfaceUrl;
/**
* 數據來源
*/
@NotNull(message = "配置文件配置必須要配置[config.source]屬性")
private String source;
/**
* ftp地址
*/
@NotNull(message = "配置文件配置必須要配置[config.ftp-host-name]屬性")
private String ftpHostName;
/**
* ftp端口
*/
@NotNull(message = "配置文件配置必須要配置[config.ftp-port]屬性")
private Integer ftpPort;
/**
* ftp用戶名
*/
@NotNull(message = "配置文件配置必須要配置[config.ftp-user-name]屬性")
private String ftpUserName;
/**
* ftp密碼
*/
@NotNull(message = "配置文件配置必須要配置[config.ftp-pass-word]屬性")
private String ftpPassWord;
/**
* ftp文件路徑
*/
@NotNull(message = "配置文件配置必須要配置[config.ftp-bash-path]屬性")
private String ftpBashPath;
/**
* 開啟掃描 open
*/
@NotNull(message = "配置文件配置必須要配置[config.is-open-scan]屬性")
private String isOpenScan;
public String getToCompanyId() {
return toCompanyId;
}
public void setToCompanyId(String toCompanyId) {
this.toCompanyId = toCompanyId;
}
public String getToUserId() {
return toUserId;
}
public void setToUserId(String toUserId) {
this.toUserId = toUserId;
}
public String getToInterfaceUrl() {
return toInterfaceUrl;
}
public void setToInterfaceUrl(String toInterfaceUrl) {
this.toInterfaceUrl = toInterfaceUrl;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getFtpHostName() {
return ftpHostName;
}
public void setFtpHostName(String ftpHostName) {
this.ftpHostName = ftpHostName;
}
public Integer getFtpPort() {
return ftpPort;
}
public void setFtpPort(Integer ftpPort) {
this.ftpPort = ftpPort;
}
public String getFtpUserName() {
return ftpUserName;
}
public void setFtpUserName(String ftpUserName) {
this.ftpUserName = ftpUserName;
}
public String getFtpPassWord() {
return ftpPassWord;
}
public void setFtpPassWord(String ftpPassWord) {
this.ftpPassWord = ftpPassWord;
}
public String getFtpBashPath() {
return ftpBashPath;
}
public void setFtpBashPath(String ftpBashPath) {
this.ftpBashPath = ftpBashPath;
}
public String getIsOpenScan() {
return isOpenScan;
}
public void setIsOpenScan(String isOpenScan) {
this.isOpenScan = isOpenScan;
}
@Override
public String toString() {
return "Config{" +
"toCompanyId='" + toCompanyId + '\'' +
", toUserId='" + toUserId + '\'' +
", toInterfaceUrl='" + toInterfaceUrl + '\'' +
", source='" + source + '\'' +
", ftpHostName='" + ftpHostName + '\'' +
", ftpPort=" + ftpPort +
", ftpUserName='" + ftpUserName + '\'' +
", ftpPassWord='" + ftpPassWord + '\'' +
", ftpBashPath='" + ftpBashPath + '\'' +
", isOpenScan='" + isOpenScan + '\'' +
'}';
}
}
二,pom.xml配置【具體版本請根據實際的springboot項目版本設置,目前我用的springboot版本是:2.4.3】
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.5.Final</version>
</dependency>
三:啟動后日志打印內容如下:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'config' to com.cdvcloud.mtysynmediaother.config.Config$$EnhancerBySpringCGLIB$$3bc107a7 failed:
Property: config.source
Value: null
Reason: 配置文件配置必須要配置[config.source]屬性
Property: config.ftpUserName
Value: null
Reason: 配置文件配置必須要配置[config.ftp-user-name]屬性
Property: config.ftpPort
Value: null
Reason: 配置文件配置必須要配置[config.ftp-port]屬性
Property: config.isOpenScan
Value: null
Reason: 配置文件配置必須要配置[config.is-open-scan]屬性
Property: config.toUserId
Value: null
Reason: 配置文件配置必須要配置[config.to-user-id]屬性
Property: config.ftpPassWord
Value: null
Reason: 配置文件配置必須要配置[config.ftp-pass-word]屬性
Property: config.ftpHostName
Value: null
Reason: 配置文件配置必須要配置[config.ftp-host-name]屬性
Property: config.ftpBashPath
Value: null
Reason: 配置文件配置必須要配置[config.ftp-bash-path]屬性
Property: config.toCompanyId
Value: null
Reason: 配置文件配置必須要配置[config.to-company-id]屬性
Property: config.toInterfaceUrl
Value: null
Reason: 配置文件配置必須要配置[config.to-interface-url]屬性
Action:
Update your application's configuration
