标签:Tricks springframework ConfigurationProperties Assertions context org import Ena
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");
Assertions.assertThat(f).isTrue();
}
@Test
public void testErrorName() {
boolean f = context.containsBean("anotherBean");
Assertions.assertThat(f).isFalse();
}
}
2. @ConfigurationProperties#prefix 不为空
@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();
}
}
标签:Tricks,springframework,ConfigurationProperties,Assertions,context,org,import,Ena 来源: https://www.cnblogs.com/zyon/p/11018505.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。