spring cloud  读取 配置文件属性值

1、bean

  1. @Data
  2. public class LocalFileConfig {
  3.  
  4. /**
  5. * 文件存储地址
  6. */
  7. private String fileServerPath;
  8.  
  9. private String fileDownloadUrl;
  10.  
  11. private String defaultCutSize;
  12.  
  13. private String fileValidateType;
  14.  
  15. private Long fileSize;
  16. }

  配置

  1. @Configuration
  2. @PropertySource(value = { "classpath:properties/oss.properties" })
  3. public class LocalFileConfigConfiguration {
  4.  
  5. @Value("${file.config.server.path}")
  6. private String fileServerPath;
  7.  
  8. @Value("${file.config.download.url}")
  9. private String fileDownloadUrl;
  10.  
  11. @Value("${file.config.default.cutSize}")
  12. private String fileDefaultCutSize;
  13.  
  14. @Value("${file.config.validate.type}")
  15. private String fileValidateType;
  16.  
  17. @Value("${file.config.validate.fileSize}")
  18. private Long fileSize;
  19.  
  20. @Bean
  21. public LocalFileConfig localFileConfig(){
  22. LocalFileConfig localFileConfig = new LocalFileConfig();
  23. localFileConfig.setFileServerPath(fileServerPath);
  24. localFileConfig.setFileDownloadUrl(fileDownloadUrl);
  25. localFileConfig.setFileValidateType(fileValidateType);
  26. localFileConfig.setDefaultCutSize(fileDefaultCutSize);
  27. localFileConfig.setFileSize(fileSize);
  28. return localFileConfig;
  29. }
  30. }

  

2、

  1. @NoArgsConstructor
    @AllArgsConstructor
    @Component
    @ConfigurationProperties(prefix = "file")
    public class FileStorageProperties {
    private String uploadDir;
  2.  
  3. public String getUploadDir() {
    return uploadDir;
    }
  4.  
  5. public void setUploadDir(String uploadDir) {
    this.uploadDir = uploadDir;
    }
    }
  6.  
  7. 网上找到第二种方法,级联应用

链接:https://www.cnblogs.com/lihaoyang/p/10223339.html

SPRINGBOOT用@CONFIGURATIONPROPERTIES获取配置文件值

SpringBoot的配置文件有yml和properties两种,看一些文章说yml以数据为中心,比较好。个人觉得properties更好用,所以这里以properties格式为例来说。

我们都知道@Value 注解可以从配置文件读取一个配置,如果只是配置某个值,比如 某一个域名,配置为xxx.domain = www.xxx.com ,这样直接在代码里用@Value获取,比较方便。

但是如果是一组相关的配置,比如验证码相关的配置,有图片验证码、手机验证码、邮箱验证码,如果想把验证码的长度做成可配置。是否能像springboot的配置,

参照着写成:

肯定是可以的!

参照源码是最好的学习方式,下面来看springboot是怎么做的

  1. server对应着一个配置类:ServerProperties(只粘贴出了部分成员变量,来说明问题)
  1.  
  1. @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
  2. public class ServerProperties
  3. implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered {
  4. /**
  5. * Server HTTP port.
  6. */
  7. private Integer port;
  8. @NestedConfigurationProperty
  9. private Compression compression = new Compression();
  10. //省略其他成员变量、getter 、setter
  1.  
  1. Compression类部分代码:
  1. public class Compression {
  2. /**
  3. * If response compression is enabled.
  4. */
  5. 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")默认从全局配置文件中获取值;

下边实现上面说的验证码配置,需要的类:

  1. 代码:
    CoreConfiguration.java
  1. @Configuration
  2. @EnableConfigurationProperties(SecurityProperties.class)
  3. public class CoreConfiguration {
  4.  
  5. //配置一些bean
  6. //@Bean
  7. //public XXXX xxxx(){}
  8. }
  1. SecurityProperties.java
  1. @ConfigurationProperties(prefix = "myapp")
  2. public class SecurityProperties {
  3.  
  4. private ValidateCodeProperties code = new ValidateCodeProperties();
  5.  
  6. public ValidateCodeProperties getCode() {
  7. return code;
  8. }
  9.  
  10. public void setCode(ValidateCodeProperties code) {
  11. this.code = code;
  12. }
  13. }
  1. ValidateCodeProperties.java
  1. public class ValidateCodeProperties {
  2.  
  3. private SmsCodeProperties sms = new SmsCodeProperties();
  4.  
  5. private ImageCodeProperties image = new ImageCodeProperties();
  6.  
  7. public SmsCodeProperties getSms() {
  8. return sms;
  9. }
  10.  
  11. public void setSms(SmsCodeProperties sms) {
  12. this.sms = sms;
  13. }
  14.  
  15. public ImageCodeProperties getImage() {
  16. return image;
  17. }
  18.  
  19. public void setImage(ImageCodeProperties image) {
  20. this.image = image;
  21. }
  22. }
  1. SmsCodeProperties.java
  1. public class SmsCodeProperties {
  2.  
  3. private int length = 4;
  4.  
  5. public int getLength() {
  6. return length;
  7. }
  8.  
  9. public void setLength(int length) {
  10. this.length = length;
  11. }
  12. }
  1. application.properties 里配置
  1. myapp.code.sms.length = 10
  1. 使用配置:
  1.   @Autowired
  2. private SecurityProperties securityProperties;
  3.  
  4. @RequestMapping("/length")
  5. public @ResponseBody String length(){
  6. int length = securityProperties.getCode().getSms().getLength();
  7. return String.valueOf(length);
  8. }

spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件的更多相关文章

  1. spring boot 解决 跨域 的两种方法 -- 前后端分离

    1.前言 以前做项目 ,基本上是使用 MVC 模式 ,使得视图与模型绑定 ,前后端地址与端口都一样 , 但是现在有些需求 ,需要暴露给外网访问 ,那么这就出现了个跨域问题 ,与同源原则冲突, 造成访问 ...

  2. spring boot 下websocket实现的两种方法

    websocket前台实现代码,保存为html执行就好 html代码来自:https://blog.csdn.net/M348915654/article/details/53616837 <h ...

  3. Spring Boot定义系统启动任务的两种方式

    Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...

  4. Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!

    在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...

  5. 创建一个 Spring Boot 项目,你会几种方法?

    我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...

  6. Spring Boot 中实现定时任务的两种方式

    在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...

  7. Spring Boot 排除自动配置的 4 种方法,关键时刻很有用!

    Spring Boot 提供的自动配置非常强大,某些情况下,自动配置的功能可能不符合我们的需求,需要我们自定义配置,这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了. 比如:数据 ...

  8. 【springboot】【socket】spring boot整合socket,实现服务器端两种消息推送

    参考地址:https://www.cnblogs.com/hhhshct/p/8849449.html

  9. 禁用 Spring Boot 中引入安全组件 spring-boot-starter-security 的方法

    1.当我们通过 maven 或 gradle 引入了 Spring boot 的安全组件 spring-boot-starter-security,Spring boot 默认开启安全组件,这样我们就 ...

随机推荐

  1. 利用 PHP CURL zip压缩文件上传

    $postData['file'] = "@".getcwd()."/../attachment/qianbao/{$customer_id}.zip"; $t ...

  2. GitHub 远程仓库 de 第一次配置

    GitHub远程仓库, Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.首先找一台电脑充当服务器的角色, 每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上 ...

  3. vuex如何实现数据持久化,刷新页面存储的值还存在

    1.安装: npm install vuex-persistedstate --save 2.找到store/index.js import Vue from 'vue' import Vuex fr ...

  4. mysql 表联结,内部联结

    mysql> select * from user; +------+----------+-----------+ | id | name | address | +------+------ ...

  5. QML学习(四)——<Text显示>

    文本显示是界面开发必不可少的内容,在Qt Quick模块中提供了 Text 项目来进行文本的显示,其中可以使用 font 属性组对文本字体进行设置.这一篇我们来看看它们的具体使用. 使用字体 就像前面 ...

  6. SDN阅读作业

    阅读文章<软件定义网络(SDN)研究进展>,并根据所阅读的文章,书写一篇博客,回答以下问题(至少3个): 1.为什么需要SDN?SDN特点? 随着网络规模的不断扩大,传统网络设备繁复的协议 ...

  7. Service Function Chaining Resource Allocation: A Survey

    摘要: 服务功能链(SFC)是未来Internet的一项关键技术. 它旨在克服当前部署模型的僵化和静态限制. 该技术的应用依赖于可以将SFC最佳映射到衬底网络的算法. 这类算法称为"服务功能 ...

  8. 解读 | 你真正理解什么是Cloud Native吗?

    你能做到每周.每天甚至每个钟头向客户发布新特性吗?新加入的开发者能够在他们工作的第一天甚至面试阶段就能部署代码吗?部署新员工的代码后,你能因为确信应用程序运行正常而安然入睡吗?建立快速发布机制,包括支 ...

  9. ProxyFactoryBean与AopProxy介绍

    1.ProxyFactoryBean的典型配置 2.进入getObject方法 /** * Return a proxy. Invoked when clients obtain beans from ...

  10. typescript - 8.命名空间

    基础 略. https://www.tslang.cn/docs/handbook/namespaces.html 多文件中的命名空间(一个文件分解为几个) 现在,我们把Validation命名空间分 ...