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。