Spring和Spring Boot开发中,常使用@ConfigurationProperties注解某个类,使其实例接受一组具有相同前缀的配置项。

可以使用@Component或Java Config将使用@ConfigurationProperties的类声明为Bean。

Spring Boot提供了@EnableConfigurationProperties也可以实现类似的注册功能。

然而@EnableConfigurationProperties注册配置Bean时,设置的Bean名称却比较特别,有以下两种情形。

1. @ConfigurationProperties#prefix 为空

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; @Data
@ConfigurationProperties // @ConfigurationProperties#prefix没有设置值的场景
public class AnotherBean {
private String anotherAttr1;
private String anotherAttr2;
private String anotherAttr3;
}

此时application.properties的配置项不需要某个特定的前缀:

another-attr1 = av1
another-attr2 = av2
another-attr3 = av3

如果使用@EnableConfigurationProperties({AnotherBean.class})将其注册为Bean时,其名字是:@ConfigurationProperties注解的类的全类名

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(properties = {"another-attr1 = v1", "another-attr2 = aaavvv2", "another-attr3 = vvvaaa3"})
public class AnotherBeanTest { @Autowired
private AnotherBean anotherBean; @Autowired
private ApplicationContext context; @Test
public void contextInitialized() {
Assertions.assertThat(anotherBean).isNotNull();
Assertions.assertThat(anotherBean.getAnotherAttr1()).isEqualToIgnoringCase("v1");
Assertions.assertThat(anotherBean.getAnotherAttr2()).isEqualToIgnoringCase("aaavvv2");
Assertions.assertThat(anotherBean.getAnotherAttr3()).isEqualToIgnoringCase("vvvaaa3");
} @Test
public void testCorrectName() {
boolean f = context.containsBean("com.dw.sb.demo.bean.AnotherBean"); // 真实Bean名称: 全类名
Assertions.assertThat(f).isTrue();
} @Test
public void testErrorName() {
boolean f = context.containsBean("anotherBean");
Assertions.assertThat(f).isFalse();
}
}

2. @ConfigurationProperties#prefix 不为空

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; @Data
@ConfigurationProperties(prefix = "demo") // @ConfigurationProperties#prefix有值的场景
public class DemoBean {
private String attr1;
private String attr2;
}

此时配置项是很熟悉的情形,需要有共同的前缀,如:

demo.attr1 = value1
demo.attr2 = value2

如果使用@EnableConfigurationProperties({DemoBean.class})将其注册为Bean,Bean的名字是

@ConfigurationProperties#prefix的值 + "-" + 注解类的全类名

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoBeanTest { @Autowired
private ApplicationContext context; @Test
public void testWrongName() {
boolean f = context.containsBean("demoBean");
Assertions.assertThat(f).isFalse();
} @Test
public void testCorrectName() {
boolean f = context.containsBean("demo-com.dw.sb.demo.bean.DemoBean"); // 真实的Bean名称:"前缀-全类名"
Assertions.assertThat(f).isTrue();
}
}

源码中,EnableConfigurationPropertiesImportSelector#ConfigurationPropertiesBeanRegistrar#getName方法实现了上述功能。

以上内容基于Spring Boot 2.0.6.RELEASE。

使用@EnableConfigurationProperties注册配置Bean时的命名规则的更多相关文章

  1. gson转换json到bean时重命名

    @Expose   @SerializedName("0001") public Map<String,ChannelBean> c0001 = new HashMap ...

  2. Quartus ii 设计中的差分信号在例化时的命名规则

    在Quartus中做设计,如果使用了差分信号的,如DDR的IP中的mem_ck与mem_ck_n,mem_dqs与mem_dqs_n,将其引入输出端口时,对其命名有一定的规则,否则就会出现错误. 如下 ...

  3. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  4. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. 通过FactoryBean配置Bean

    这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...

  6. 使用外部属性文件配置Bean以及Bean的生命周期方法

    1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...

  7. 4.spriing:Bean的生命周期/工厂方法配置Bean/FactoryBean

    1.Bean的生命周期 scope:singleton/prototype 1)spring容器管理singleton作用的生命周期,spring能够精确知道Bean合适创建,何时初始化完成,以及何时 ...

  8. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  9. Spring配置 bean

    在 Spring 的 IOC 容器里配置 Bean <bean id="helloWorld" class="com.xiya.spring.beans.Hello ...

随机推荐

  1. 大龄程序员的出路在哪里?八年老Android的一点心得

    这篇文章,给大家聊聊Android工程师的职业发展规划的一些思考,同时也给不少20多岁.30多岁,但是对自己的职业未来很迷茫的同学一些建议. 笔者希望通过此文,帮大家梳理一下程序员的职业发展方向,让大 ...

  2. 第2篇-JVM虚拟机这样来调用Java主类的main()方法

    在前一篇 第1篇-关于JVM运行时,开篇说的简单些 中介绍了call_static().call_virtual()等函数的作用,这些函数会调用JavaCalls::call()函数.我们看Java类 ...

  3. 基于Gitea搭建属于自己的Git服务

    作者:IT王小二 博客:https://itwxe.com 一.搭建环境和前提 搭建环境: 操作系统:CentOS7.6 Docker版本:docker-ce-18.09.9 Lsky Pro版本:1 ...

  4. 基于ScheduledExecutorService的并发定时任务处理能力测试

    测试代码 定时器类 package business.util; import java.util.concurrent.Executors; import java.util.concurrent. ...

  5. Fluid + GooseFS 助力云原生数据编排与加速快速落地

    前言 Fluid 作为基于 Kubernetes 开发的面向云原生存算分离场景下的数据调度和编排加速框架,已于近期完成了 v0.6.0 版本的正式发布.腾讯云容器 TKE 团队一直致力于参与 Flui ...

  6. [数据结构]ODT(珂朵莉树)实现及其应用,带图

    [数据结构]ODT(珂朵莉树)实现及其应用,带图 本文只发布于博客园,其他地方若出现本文均是盗版 算法引入 需要一种这样的数据结构,需要支持区间的修改,区间不同值的分别操作. 一般的,我们会想到用线段 ...

  7. CVE-2020-2883漏洞复现&&流量分析

    CVE-2020-2883漏洞复现&&流量分析 写在前面 网上大佬说CVE-2020-2883是CVE-2020-2555的绕过,下面就复现了抓包看看吧. 一.准备环境 靶机:win7 ...

  8. NOIP 模拟 $31\; \rm Cover$

    题解 \(by\;zj\varphi\) 因为对于所有区间,都只有包含和被包含关系,这就是一个树形结构. 设 \(\rm f_{i,j}\) 表示在第 \(\rm i\) 个节点,最多被覆盖 \(\r ...

  9. HTTP协议之:HTTP/1.1和HTTP/2

    目录 简介 HTTP/1.1 HTTP/2 传输模式对比 流优先级 缓冲区溢出处理 预测资源请求 压缩 总结 简介 HTTP的全称是Hypertext Transfer Protocol,是在1989 ...

  10. 一款优秀的国产性能测试工具kylinPET在麒麟操作系统上的能力表现

    一直以来人们从事性能测试,使用最多的是Jmeter和LoadRuner .笔者在网上找了一下国产性能测试工具,从中筛选出一款优秀的国产的性能测试工具kylinPET.查找该款工具的历史,好像有十年历史 ...