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。

最新文章

  1. Monyer.cn黑客小游戏
  2. Protocol Buffers(Protobuf)开发者指南---概览
  3. 彻底理解数字图像处理中的卷积-以Sobel算子为例
  4. C# 新技巧(一)
  5. 在JavaScript 自定义对象来模拟Java中的Map
  6. 使用HackRF+GNU Radio 破解吉普车钥匙信号
  7. 由“大数据量Excel入库高效方式”瞥见“并联系统”之优势
  8. OpenXML_导入Excel到数据库
  9. Linux命令之find(一)
  10. 探讨 : Host在IIS上的WCF Service的执行方式
  11. 复习php的一些函数
  12. vue实现打印功能的两种方法
  13. linux服务基础(二)之httpd2.2基础配置
  14. macos 下通过sublime text 3 + gosublime+sublimegdb 开发注意事项
  15. docker同步时区时间
  16. Vue入门系列(五)Vue实例详解与生命周期
  17. cmd 运行(打包后的)java程序
  18. iframe 问题集合
  19. HIVE之 Sqoop 1.4.6 安装、hive与oracle表互导
  20. Java基础教程(15)--枚举类型

热门文章

  1. TCP 才不傻!
  2. 如何高效、快速学习Flutter?如何避坑?(文末送服福利)
  3. netty系列之:文本聊天室
  4. 常用的Windows快捷键
  5. Linux命令(一)之目录结构、Linux终端操作、关机重启等一些基本操作
  6. dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级
  7. IDEA Maven快速创建JavaWeb项目
  8. Git-02-版本回退
  9. 【网络编程】TCPIP_3_地址族与数据序列
  10. 一次关于shiro反序列化漏洞的思考