SpringBoot @ConfigurationProperties注解的简单使用

源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationProperties {
    @AliasFor("prefix")
    String value() default "";

    @AliasFor("value")
    String prefix() default "";

    boolean ignoreInvalidFields() default false;

    boolean ignoreUnknownFields() default true;
}

使用

向注解中传入配置文件中的前缀名,如果配置文件如下:

myConfigs:
  config1:
    field1: f1
    field2: f2
    field3: f3

那么代码中的配置类应该这样写:

@Component
@ConfigurationProperties("myConfigs.config1")
public class MyConfig1 {
    String field1;
    String field2;
    String field3;
}

如上所示,field1, field2, field3三个属性就被绑定到了对象上。

注意到我们使用了@Component,实际上我们使用配置类都是将其注入到其他类中,所以我们往往将其注册为Bean。

ignoreInvalidFields默认为false,不合法的属性的属性会默认抛出异常;
ignoreUnknownFields默认为true, 未能识别的属性会被忽略(所以打错了名字就会被忽略了)

@ConfigurationProperties(prefix="config.prefix", ignoreInvalidFields=true, ignoreUnknownFields=false)
public class MyConfig {
    // fields
}

Spring Boot的绑定规则相当宽松,myField, my-field, my_field等都能识别绑定到myField上。

可以给字段设定默认值,这样配置中没有传入时会使用默认值。

@ConfigurationProperties("your.prefix")
public class YourConfig {
    private String field = "Default"
    // setter
}

类的字段必须要有public访问权限的setter方法。

在很多情况下public的setter方法时必须的,使用IDEA的话,这里推荐Alt+Insert(Windows, Mac使用Alt+n)生成;当然,想使用Lombok也可以

以上就是SpringBoot @ConfigurationProperties注解的简单使用的详细内容,更多关于SpringBoot @ConfigurationProperties注解的资料请关注我们其它相关文章!

(0)

相关推荐

  • springboot中使用@Transactional注解事物不生效的坑

    一:在springboot中使用事物遇到的坑 1.我们知道spring中的事物分为两种:一种是编程式事物,一种是声明式事物.顾名思义,编程式事物是指通过代码去实现事物管理,这里不做过多说明.另一种是声明式事物,分为两种情况01:一种是通过传统xml方式配置,02:使用@Transaction注解方式配置,这是主要讲解的是通过注解方式配置.因为在springboot项目中,会自动配置DataSourceTransactionManager,我们只需要在对应的方法上或者类上加上@Transactio

  • 基于注解的springboot+mybatis的多数据源组件的实现代码

    通常业务开发中,我们会使用到多个数据源,比如,部分数据存在mysql实例中,部分数据是在oracle数据库中,那这时候,项目基于springboot和mybatis,其实只需要配置两个数据源即可,只需要按照 dataSource -SqlSessionFactory - SqlSessionTemplate配置好就可以了. 如下代码,首先我们配置一个主数据源,通过@Primary注解标识为一个默认数据源,通过配置文件中的spring.datasource作为数据源配置,生成SqlSessionF

  • SpringBoot中自定义注解实现参数非空校验的示例

    前言 由于刚写项目不久,在写 web 后台接口时,经常会对前端传入的参数进行一些规则校验,如果入参较少还好,一旦需要校验的参数比较多,那么使用 if 校验会带来大量的重复性工作,并且代码看起来会非常冗余,所以我首先想到能否通过一些手段改进这点,让 Controller 层减少参数校验的冗余代码,提升代码的可阅读性. 经过阅读他人的代码,发现使用 annotation 注解是一个比较方便的手段,SpringBoot 自带的 @RequestParam 注解只会校验请求中该参数是否存在,但是该参数是

  • springBoot系列常用注解(小结)

    @PropertySource 作用是:对自定义的properties文件加载 使用:@PropertySource(value={"classpath:people.properties"})或者@PropertySource(value="classpath:people.properties") properties文件,获取到值乱码问题 乱码解决: file ->settings -->file encoding--> 勾选Transpar

  • SpringBoot自定义注解实现Token校验的方法

    1.定义Token的注解,需要Token校验的接口,方法上加上此注解 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementTyp

  • SpringBoot启动类@SpringBootApplication注解背后的秘密

    在用SpringBoot的项目的时候,会发现不管干什么都离不开启动类,他是程序唯一的入口,那么他究竟为我们做了什么?本篇文章主要解析@SpringBootApplication. 一.启动类 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } } 二.@SpringBoo

  • SpringBoot 拦截器和自定义注解判断请求是否合法

    应用场景举例: 当不同身份的用户请求一个接口时,用来校验用户某些身份,这样可以对单个字段数据进行精确权限控制,具体看代码注释 自定义注解 /** * 对比请求的用户身份是否符合 * @author liuyalong * @date 2020/9/25 16:03 */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface CompareUser { /** * The name

  • SpringBoot基于自定义注解实现切面编程

    1.相关依赖包 <!-- aop 依赖包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <ar

  • SpringBoot自定义注解API数据加密和签名校验

    api数据数据签名(MD5,SHA1) 签名枚举类SginEnum.java package com.jx.app.api.framework.annotation.enums; /** * @ClassName: SginEnum * @Description: TODO(这是一个签名枚举类) * @author gangyu * @date 2018年11月20日 下午4:30:44 */ public enum SginEnum { //0不需要签名,1使用MD5数据加密 2 使用SHA数

  • 基于注解实现 SpringBoot 接口防刷的方法

    该示例项目通过自定义注解,实现接口访问次数控制,从而实现接口防刷功能,项目结构如下: 一.编写注解类 AccessLimit package cn.mygweb.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Targ

  • SpringBoot @ConfigurationProperties使用详解

    简介 本文将会详细讲解@ConfigurationProperties在Spring Boot中的使用. 添加依赖关系 首先我们需要添加Spring Boot依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <relativePath/> <!-- lookup

随机推荐