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面试官:说说你对 Binder 驱动的了解?

    面试官提了一个问题:说说你对 binder 驱动的了解.这个问题虽有些 "面试造火箭" 的无奈,可难点就是亮点.价值所在,是筛选面试者的有效手段.如果让你回答,你能说出多少呢?我们 ...

  2. 【javaFX学习】(二) 面板手册

    移至http://blog.csdn.net/qq_37837828/article/details/78732591 更新 找了好几个资料,没找到自己想要的,自己整理下吧,方便以后用的时候挑选,边学 ...

  3. Devcpp(Dev-C++)代码编辑的快捷键

    转自:https://blog.csdn.net/u010940020/article/details/43735549 这里记录一些个人使用Devcpp时,摸索出来的代码编辑快捷键,感觉非常有用.如 ...

  4. Activity与Service生命周期

    一. Activity 先展示一张Activity的生命周期图: 1.1 Activity状态 只有下面三个状态是静态的,可以存在较长的时间内保持状态不变.(其它状态只是过渡状态,系统快速执行并切换到 ...

  5. Required request body is missing-请求接口报错

    一.问题由来 自己目前在做一个小程序的后台,已经写好了项目中的很多的接口,同时也在进行一些修改,比如添加拦截器,统一校验一个固定的参数是否正确. 在自己添加拦截器之前,这些接口都可以正常访问,可是在添 ...

  6. @ConfigurationProperties实现自定义配置绑定

    @ConfigurationProperties使用 创建一个类,类名上方注解,配置prefix属性,如下代码: @ConfigurationProperties( prefix = "he ...

  7. 攻防世界PWN简单题 level0

    攻防世界PWN简单题 level0 开始考验栈溢出的相关知识了 Checksec 一下文件 看看都开了什么保护 和 是多少位的程序 发现是64位的程序, 扔进IDA64.IDA YYDS.. 进入主函 ...

  8. noip9

    T1 本次考试最水的一道题,然而我sb,前一个小时,找了一大堆跟题目无关的性质,干脆打了个20pts的表,然后就走了,最后几分钟才看出来,匆匆码出来,结果段错误,然后考试就结束了. 好吧,段错误是UB ...

  9. c# 执行python方法

    在C#使用Python脚本文件要注意的的是,首先要将IronPython2.7安装路径中的两个dll文件添加到C#引用中一个是IronPython.dll,另一个是Microsoft.Scriptin ...

  10. WPF 显示3D密集场景,堆场管理系统

    又好久好久没写博客了,这次接着上文https://www.cnblogs.com/CSSZBB/p/12785380.html,上文用WPF 的绘图功能,制作了一个伪3D的2.5D控件ThreeDBo ...