春の雲は、プロファイルのプロパティ値を読み込みます
1、豆
@data パブリッククラスLocalFileConfig { / ** *文件存储地址 * / プライベート文字列fileServerPath。 プライベート文字列fileDownloadUrl。 プライベート文字列defaultCutSize。 プライベート文字列fileValidateType。 プライベートロングファイルサイズ。 }
コンフィギュレーション
@Configuration @PropertySource(値= { "クラスパス:プロパティ/ oss.properties"}) パブリッククラスLocalFileConfigConfiguration { @value( "$ {file.config.server.path}") プライベート文字列fileServerPath。 @value( "$ {file.config.download.url}") プライベート文字列fileDownloadUrl。 @value( "$ {file.config.default.cutSize}") プライベート文字列fileDefaultCutSize。 @value( "$ {file.config.validate.type}") プライベート文字列fileValidateType。 @value( "$ {file.config.validate.fileSize}") プライベートロングファイルサイズ。 @Bean 公共LocalFileConfig localFileConfig(){ localFileConfig.setFileServerPath(fileServerPath)。 localFileConfig.setFileDownloadUrl(fileDownloadUrl)。 localFileConfig.setFileValidateType(fileValidateType)。 localFileConfig.setDefaultCutSize(fileDefaultCutSize)。 localFileConfig.setFileSize(ファイルサイズ)。 localFileConfigを返します。 } }
2、
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(接頭辞= "ファイル")
パブリッククラスFileStorageProperties {
プライベート文字列のuploaddir;
パブリック文字列getUploadDir(){
戻りuploaddir;
}
パブリック無効setUploadDir(文字列のuploaddir){
this.uploadDir = uploaddir;
}
}
オンライン第二の方法を見つけ、カスケードアプリケーション
リンク:https://www.cnblogs.com/lihaoyang/p/10223339.html
SPRINGBOOTは、設定ファイルの値を取得@CONFIGURATIONPROPERTIES
より良い、いくつかの記事はYMLデータ中心言う見て、SpringBootプロファイルはYML持っているとプロパティが2つです。個人的に私はので、ここでのプロパティの形式に一例であり、優れた特性を持つことを感じます。
だけなので、コード内で直接@valueを慣れるxxx.domain = www.xxx.comに構成され、このような特定のドメイン名と値を、、、設定している場合我々は、すべての比較、@valueノートは、構成プロファイルから読み取ることができます知っています便利。
それは、このようなコード関連の設定など関連する構成のグループがある場合は、認証コードの長さが設定可能であることを確認したい場合でも、コード、電話コード、メールアドレス認証コード検証画像は、あります。springboot構成としてかどうか、
以下のように記述を参照します:
確かにそう!
リファレンスソースは、見てみましょうのspringbootを行う方法で、学習するための最良の方法であります
カテゴリ構成サーバーへの対応:ServerProperties(問題を説明するために、部分的にしかメンバ変数を貼り付け)
@ConfigurationProperties(接頭辞= "サーバー"、ignoreUnknownFields =真) パブリッククラスServerProperties 実装EmbeddedServletContainerCustomizer、EnvironmentAware、{発注 / ** *サーバのHTTPポート。 * / プライベート整数ポート。 @NestedConfigurationProperty プライベート圧縮圧縮=新しい圧縮(); //省略其他成员变量、ゲッター、セッター
コードの圧縮ベースの一部:
パブリッククラス圧縮{ / ** 応答圧縮が有効になっている場合*。 * / プライベートブール値は=有効false;
お読みになった後は非常に明確にする必要があり、その理由ははserver.port = 8081、server.display名=書き込むことができ 、lhyapp、server.compression.enabled =真ので ServerProperties クラスの使用
@ConfigurationProperties(接頭辞=「サーバー」、 ignoreUnknownFields =真) server.compression.enabled =真のあなたは、このタイプのような何かをしたい場合は、接頭辞接頭辞の設定ファイルを指定し、アノテーションは、次に我々は、クラスの評判を圧縮し、必要ServerProperties このクラスで参照、属性名は、構成プロファイルの名前に対応しています。
@ConfigurationProperties:
SpringBoot対応する設定バインドのために、このクラスのすべての属性と設定ファイルを知らせる;
接頭辞=「XXX」:以下のすべてが1つのマッピング属性れるプロファイルは
唯一このコンポーネントがコンテナを提供するために、アセンブリのコンテナです@ConfigurationPropertiesは機能;
@ConfigurationProperties(接頭辞=「XXX」)はグローバルデフォルトの設定ファイルから値を取得します。
上に述べた認証コード構成を実現するために、次の、あなたはクラスが必要:
コード:
CoreConfiguration.java
@Configurationの @EnableConfigurationProperties(SecurityProperties.class) パブリッククラスCoreConfiguration { //配置一些ビーン ビーン@ // //パブリックXXXXのXXXX(){} }
SecurityProperties.java
@ConfigurationProperties(接頭辞= "MyAppの") パブリッククラスSecurityProperties { プライベートValidateCodePropertiesコード=新しいValidateCodeProperties()。 公共ValidateCodePropertiesにgetCode(){ 戻りコード。 } 公共ボイドSETCODE(ValidateCodePropertiesコード){ this.code =コード。 } }
ValidateCodeProperties.java
public class ValidateCodeProperties { private SmsCodeProperties sms = new SmsCodeProperties(); private ImageCodeProperties image = new ImageCodeProperties(); public SmsCodeProperties getSms() { return sms; } public void setSms(SmsCodeProperties sms) { this.sms = sms; } public ImageCodeProperties getImage() { return image; } public void setImage(ImageCodeProperties image) { this.image = image; } }
SmsCodeProperties.java
public class SmsCodeProperties { private int length = 4; public int getLength() { return length; } public void setLength(int length) { this.length = length; } }
在application.properties 里配置
myapp.code.sms.length = 10
使用配置:
@Autowired private SecurityProperties securityProperties; @RequestMapping("/length") public @ResponseBody String length(){ int length = securityProperties.getCode().getSms().getLength(); return String.valueOf(length); }
SpringBoot的配置文件有yml和properties两种,看一些文章说yml以数据为中心,比较好。个人觉得properties更好用,所以这里以properties格式为例来说。
我们都知道@Value 注解可以从配置文件读取一个配置,如果只是配置某个值,比如 某一个域名,配置为xxx.domain = www.xxx.com ,这样直接在代码里用@Value获取,比较方便。
但是如果是一组相关的配置,比如验证码相关的配置,有图片验证码、手机验证码、邮箱验证码,如果想把验证码的长度做成可配置。是否能像springboot的配置,
参照着写成:
肯定是可以的!
参照源码是最好的学习方式,下面来看springboot是怎么做的
server对应着一个配置类:ServerProperties(只粘贴出了部分成员变量,来说明问题)
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered { /** * Server HTTP port. */ private Integer port; @NestedConfigurationProperty private Compression compression = new Compression(); //省略其他成员变量、getter 、setter
Compression类部分代码:
public class Compression { /** * If response compression is enabled. */ private boolean enabled = false;
看过后应该很明白了,之所以能写成server.port=8081,server.display-name=lhyapp,server.compression.enabled=true ,是因为 ServerProperties 类上使用了
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) 注解,其中prefix 指定配置文件里的前缀, 如果想弄成这样式的 server.compression.enabled = true ,就需要再声名一个类 Compression ,然后在ServerProperties 中引用这个类,属性名对应配置文件中的配置名。
@ConfigurationProperties:
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
prefix = "xxx":配置文件中哪个下面的所有属性进行一一映射
只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
@ConfigurationProperties(prefix = "xxx")默认从全局配置文件中获取值;
下边实现上面说的验证码配置,需要的类:
代码:
CoreConfiguration.java
@Configuration @EnableConfigurationProperties(SecurityProperties.class) public class CoreConfiguration { //配置一些bean //@Bean //public XXXX xxxx(){} }
SecurityProperties.java
@ConfigurationProperties(prefix = "myapp") public class SecurityProperties { private ValidateCodeProperties code = new ValidateCodeProperties(); public ValidateCodeProperties getCode() { return code; } public void setCode(ValidateCodeProperties code) { this.code = code; } }
ValidateCodeProperties.java
public class ValidateCodeProperties { private SmsCodeProperties sms = new SmsCodeProperties(); private ImageCodeProperties image = new ImageCodeProperties(); public SmsCodeProperties getSms() { return sms; } public void setSms(SmsCodeProperties sms) { this.sms = sms; } public ImageCodeProperties getImage() { return image; } public void setImage(ImageCodeProperties image) { this.image = image; } }
SmsCodeProperties.java
パブリッククラスSmsCodeProperties { プライベートint型の長さ= 4。 公共INTのgetLength(){ 戻り値の長さ; } 公共ボイドはsetLength(int型の長さ){ this.length =長さ。 } }
application.propertiesにおける構成で
myapp.code.sms.length = 10
コンフィギュレーションを使用します。
@Autowired プライベートSecurityPropertiesのsecurityProperties。 ( "/長さ")@RequestMapping 公衆@ResponseBody文字列の長さ(){ int型の長さ= securityProperties.getCode()getSms()のgetLength()。。。 String.valueOf(長さ)を返します。 }