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

 
 
 

 

SpringBoot用@ConfigurationProperties获取配置文件值的更多相关文章

  1. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  2. Spring Boot入门(二):获取配置文件值

    本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...

  3. springboot拦截器中获取配置文件值

    package com.zhx.web.interceptor; import com.zhx.util.other.IpUtil; import org.slf4j.Logger; import o ...

  4. SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式

    Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...

  5. JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理

    转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较   @Confi ...

  6. SpringBoot获取配置文件,就这么简单。

    在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...

  7. spring-boot -配置文件值注入

    /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定: 默认在 ...

  8. Spring Boot之配置文件值注入(@ConfigurationProperties)

    前言:Spring Boot配置文件值的注入有两种方式,分别是 @ConfigurationProperties @Value 这里我们使用第一种 首先我们创建一个application.yml文件, ...

  9. @Profile使用及SpringBoot获取profile值

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Fmuma/article/details ...

随机推荐

  1. Linux系统各发行版镜像下载(借阅)

    Linux各个版本资源下载 Linux系统各发行版镜像下载(持续更新) == Linux系统各发行版镜像下载(2014年10月更新),如果直接下载不了,请使用迅雷下载.并且注意,我的下载地址,在  迅 ...

  2. 回文(palindrome)

    如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文).

  3. sys/time.h 和 time.h

    今天在燕麦工作第二天.看荣哥给我的程序,发现程序里面用的延时跟我以前使用的不同.导入两个头文件,然后用函数来获得时间.关于这个函数特别查来一下. time.h  是ISO C99 标准日期头文件. s ...

  4. Spring的两种代理JDK和CGLIB的区别浅谈

    一.原理区别: java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理. 而cglib动态代理是利用asm开源包,对代理对象类的class文件 ...

  5. C#、.Net经典面试题目及答案

    1 请你说说   .NET  中类和结构的区别? 答:结构和类具有大体的语法,但是结构受到的限制比类要多.结构不能申明有默认的构造函数,为结构的副本是又编译器创建 和销毁的,所以不需要默认的构造函数和 ...

  6. Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

    出错原因:在查询整个sqlite数据库时,没有查询到 "_id" 这一列. 原来的代码是:mSQLiteDatabase.query(table_name, new String[ ...

  7. 你真的会Xilinx FPGA的复位吗?

    Get Smart About Reset: Think Local, Not Global. 对于复位信号的处理,为了方便我们习惯上采用全局复位,博主在很长一段时间内都是将复位信号作为一个I/O口, ...

  8. linux-python在vim下的自动补全功能

    linux-python在vim下的自动补全功能 安装配置: wget https://github.com/rkulla/pydiction/archive/master.zipunzip -q m ...

  9. Java学习--数组与方法

    1. public class MethodDemo01{ public static void main(String args[]){ printInfo() ; // 调用printInfo() ...

  10. Android常用库和插件

    下拉刷新 PullLoadMoreRecyclerView 实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性.网格.瀑布流效果演示 https://github.co ...