spring cloud read profile property values
1、bean
@Data public class LocalFileConfig { /** * 文件存储地址 */ private String fileServerPath; private String fileDownloadUrl; private String defaultCutSize; private String fileValidateType; private Long fileSize; }
Configuration
@Configuration @PropertySource(value = { "classpath:properties/oss.properties" }) public class LocalFileConfigConfiguration { @Value("${file.config.server.path}") private String fileServerPath; @Value("${file.config.download.url}") private String fileDownloadUrl; @Value("${file.config.default.cutSize}") private String fileDefaultCutSize; @Value("${file.config.validate.type}") private String fileValidateType; @Value("${file.config.validate.fileSize}") private Long fileSize; @Bean public LocalFileConfig localFileConfig(){ LocalFileConfig localFileConfig = new LocalFileConfig(); localFileConfig.setFileServerPath (fileServerPath); localFileConfig.setFileDownloadUrl(fileDownloadUrl); localFileConfig.setFileValidateType(fileValidateType); localFileConfig.setDefaultCutSize(fileDefaultCutSize); localFileConfig.setFileSize(fileSize); return localFileConfig; } }
2、
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties (prefix = "File")
public class FileStorageProperties {
Private String uploaddir;
public String getUploadDir () {
return uploaddir;
}
public void setUploadDir (String uploaddir) {
this.uploadDir = uploaddir;
}
}
Online find the second method, the cascade application
Links: https://www.cnblogs.com/lihaoyang/p/10223339.html
SPRINGBOOT get the configuration file value @CONFIGURATIONPROPERTIES
SpringBoot profiles have yml and properties are two, to see some articles say yml data-centric, better. Personally I feel that with better properties, so here is an example to properties format.
We all know @Value notes can be read from a configuration profile, if only configure a value, such as a particular domain name, configured to xxx.domain = www.xxx.com, so get used @Value directly in the code, compare Convenience.
But if it is a group of related configuration, such as code-related configuration, there are pictures verification code, phone code, email address Verification Code, if you want to make a verification code length is configurable. Whether as springboot configuration,
Referring to the written as:
Definitely yes!
Reference source is the best way to learn, let's look springboot is how to do
category corresponds to a configuration server: ServerProperties (paste only a partial member variables, to illustrate the problem)
@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-based part of the code:
public class Compression { /** * If response compression is enabled. */ private boolean enabled = false;
After reading should be very clear, and the reason can be written server.port = 8081, server.display-name = lhyapp, server.compression.enabled = true, because ServerProperties the class uses
@ConfigurationProperties (prefix = "server", ignoreUnknownFields = true) annotation, which specifies the prefix prefix configuration file, if you want something like this type of server.compression.enabled = true, then we need a class reputation Compression, then ServerProperties referenced in this class, attribute name corresponds to the name of the configuration profile.
@ConfigurationProperties:
Tell SpringBoot all the attributes and configuration files in this class for the corresponding configuration bind;
prefix = "xxx": profile in which all of the following attributes one mapping
only this component is a container assembly, in order to provide container the @ConfigurationProperties function;
@ConfigurationProperties (prefix = "XXX") get the value from the global default configuration file;
Below to achieve the above said verification code configuration, you need the class:
Code:
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
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
public class SmsCodeProperties { private int length = 4; public int getLength() { return length; } public void setLength(int length) { this.length = length; } }
In the configuration in application.properties
myapp.code.sms.length = 10
Use the configuration:
@Autowired private SecurityProperties securityProperties; @RequestMapping("/length") public @ResponseBody String length(){ int length = securityProperties.getCode().getSms().getLength(); return String.valueOf(length); }