使用@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。

posted on   CokeCode  阅读(2343)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示