spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件
spring cloud 读取 配置文件属性值
1、bean
@Data
public class LocalFileConfig { /**
* 文件存储地址
*/
private String fileServerPath; private String fileDownloadUrl; private String defaultCutSize; private String fileValidateType; private Long fileSize;
}
配置
@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;
}
} 网上找到第二种方法,级联应用
链接: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是怎么做的
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;
}
}

在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);
}

spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件的更多相关文章
- spring boot 解决 跨域 的两种方法 -- 前后端分离
1.前言 以前做项目 ,基本上是使用 MVC 模式 ,使得视图与模型绑定 ,前后端地址与端口都一样 , 但是现在有些需求 ,需要暴露给外网访问 ,那么这就出现了个跨域问题 ,与同源原则冲突, 造成访问 ...
- spring boot 下websocket实现的两种方法
websocket前台实现代码,保存为html执行就好 html代码来自:https://blog.csdn.net/M348915654/article/details/53616837 <h ...
- Spring Boot定义系统启动任务的两种方式
Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...
- Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...
- 创建一个 Spring Boot 项目,你会几种方法?
我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- Spring Boot 排除自动配置的 4 种方法,关键时刻很有用!
Spring Boot 提供的自动配置非常强大,某些情况下,自动配置的功能可能不符合我们的需求,需要我们自定义配置,这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了. 比如:数据 ...
- 【springboot】【socket】spring boot整合socket,实现服务器端两种消息推送
参考地址:https://www.cnblogs.com/hhhshct/p/8849449.html
- 禁用 Spring Boot 中引入安全组件 spring-boot-starter-security 的方法
1.当我们通过 maven 或 gradle 引入了 Spring boot 的安全组件 spring-boot-starter-security,Spring boot 默认开启安全组件,这样我们就 ...
随机推荐
- 完美兼容IE10以下所有版本
IE一直是个恶心东西 各种不支持 现在发现了个好东西可以兼容ie10以下所有浏览器 <!--[if lte IE 9]><script>window.location.href ...
- Configure JSON.NET to ignore DataContract/DataMember attributes
https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...
- [转]查看 docker 容器使用的资源
作者:sparkdev 出处:http://www.cnblogs.com/sparkdev/ 在容器的使用过程中,如果能及时的掌握容器使用的系统资源,无论对开发还是运维工作都是非常有益的.幸 ...
- static final与final修饰的常量有什么不同
最近重头开始看基础的书,对一些基础的概念又有了一些新的理解,特此记录一下 static final修饰的常量: 静态常量(static修饰的全部为静态的),编译器常量,编译时就确定其值(java代码经 ...
- mysql 创建主键,修改主键
//添加一个字段pid并且设置为主键(auto_increment)自增(auto_increment),不可为null,类型为int unsigned alter table table1 add ...
- hive 整合ranger
一.安装hive插件 1.解压安装 # tar zxvf ranger-2.0.0-SNAPSHOT-hive-plugin.tar.gz -C /data1/hadoop/ 2.修改install ...
- Ecms7.5版CK编辑器保留word格式如何修改
7.5版的编辑器默认会清除多余的word代码,如果要保留word格式怎么修改? CKeditor编辑器默认复制会清除多余word代码,如果要保留word格式可以按下面修改配置: 修改 /e/admin ...
- K8S从入门到放弃
K8S介绍相关 kubernetes(K8S)集群及Dashboard安装配置 kubernetes(K8S)创建自签TLS证书 K8S Kubernetes 架构 K8S组件 K8S API对象 K ...
- YoTube 视频如何下载
因我学习自动化测试 ,国内的C# selenium 搭建的环境的资料甚少,然后去国外网站找资料, 曹鼠给我的gogle安装一个下载YoTube视频插件,特此非常感谢他. 前提条件需要一个服务器:Sha ...
- Server 2003 操作系统位数
安装好电脑系统,如何查看windows 2003/xp/win7是64位还是32位? 方法/步骤 第一种方法:桌面上鼠标右键单击“计算机”(我的电脑) 在弹出的快捷菜单中选择“属性”,如果看到64的字 ...