使用@EnableConfigurationProperties注册配置Bean时的命名规则
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时的命名规则的更多相关文章
- gson转换json到bean时重命名
@Expose @SerializedName("0001") public Map<String,ChannelBean> c0001 = new HashMap ...
- Quartus ii 设计中的差分信号在例化时的命名规则
在Quartus中做设计,如果使用了差分信号的,如DDR的IP中的mem_ck与mem_ck_n,mem_dqs与mem_dqs_n,将其引入输出端口时,对其命名有一定的规则,否则就会出现错误. 如下 ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 通过FactoryBean配置Bean
这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- 4.spriing:Bean的生命周期/工厂方法配置Bean/FactoryBean
1.Bean的生命周期 scope:singleton/prototype 1)spring容器管理singleton作用的生命周期,spring能够精确知道Bean合适创建,何时初始化完成,以及何时 ...
- 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 ...
- Spring配置 bean
在 Spring 的 IOC 容器里配置 Bean <bean id="helloWorld" class="com.xiya.spring.beans.Hello ...
随机推荐
- dubbo学习实践(2)之Dubbo入门Demo
开篇之前,先来了解下dubbo服务治理与技术架构,直接看图 dubbo技术架构图: 1. 新建dubbo项目,目录结构如下 代码说明: 1.代码分为Provider(服务提供方)与consumer(服 ...
- CYPEESS USB3.0程序解读之---同步FIFO(slaveFifoSync)
上一篇文章解读了CYPRESS FX3的GPIO的操作过程,下面解读同步FIFO的一个例子(slaveFifoSync). *生产者,消费者. 1.首先看DMA的回调函数(cyu3dma.h): ty ...
- jd-gui或jad反编译工具bug
文件1:A.class------------2020-09-01日版本 文件2:A.class------------2020-09-02日版本,代码内容有变动. bug出现条件:文件1或文件2同目 ...
- Java Slf4j日志配置输出到文件中
1.概述 新项目需要增加日志需求,所以网上找了下日志配置,需求是将日志保存到指定文件中.网上找了下文章,发现没有特别完整的文章,下面自己整理下. 1.Java日志概述 对于一个应用程序来说日志记录是必 ...
- C#与.NET、CLR、CLI是什么关系?什么是.NET框架
1.C#与.NET.CLR.CLI是什么关系?什么是.NET框架? 这个问题好专业啊!一句话两句话还真不好说清.您听说过C++中有个COM的概念吧?您听说过JAVA里的虚拟机吧?CLR(公共 ...
- 关于腾讯云redis 无法外网访问的解决方案
问题简介: 今天购买了一台腾讯云的redis:如图 可是我没有找到 腾讯云提供的外网地址,我该怎么连接呢?百度了一大堆 全部是 在腾讯云服务器上搭建的Redis实例的解决办法.完全不匹配. 开始解决: ...
- 工具库用久了,你还会原生操作 Cookie 吗?
用得好了,工具库和框架确实是一大助力,但就怕我们会因此习惯了走捷径,而忘了自己的根本依靠是什么. 前言 前端技术的飞速发展,给从业人员不可避免地带来了"疲劳"感,我们常常会感叹学不 ...
- VS在调试桌面程序时,cout到控制台方法
参考博客:https://blog.csdn.net/xinxinsky/article/details/80733400 C++桌面程序设置 Properties -> Build Event ...
- VMware ESXi 7.0 U2 SLIC & Unlocker Intel NUC 专用镜像 202109 更新
2021.08.31 更新:集成 "vmkusb-nic-fling" 和 "nvme-community",现在只有一个镜像. 2021.06.16 更新:集 ...
- Java同步之线程池详解
带着问题阅读 1.什么是池化,池化能带来什么好处 2.如何设计一个资源池 3.Java的线程池如何使用,Java提供了哪些内置线程池 4.线程池使用有哪些注意事项 池化技术 池化思想介绍 池化思想是将 ...