org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry#example.scannable.DefaultNamedComponent源码实例Demo

下面列出了org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry#example.scannable.DefaultNamedComponent 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void generateBeanNameWithDefaultNamedComponent() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertNotNull("The generated beanName must *never* be null.", beanName);
	assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
	assertEquals("thoreau", beanName);
}
 
private void testDefault(ClassPathScanningCandidateComponentProvider provider,
		Class<? extends BeanDefinition> expectedBeanDefinitionType) {
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
	assertTrue(containsBeanClass(candidates, StubFooDao.class));
	assertTrue(containsBeanClass(candidates, NamedStubDao.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertEquals(7, candidates.size());
	assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
 
@Test
public void customSupportIncludeFilterWithNonIndexedTypeUseScan() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
	// This annotation type is not directly annotated with Indexed so we can use
	// the index to find candidates
	provider.addIncludeFilter(new AnnotationTypeFilter(CustomStereotype.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
	assertEquals(1, candidates.size());
	assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
}
 
@Test
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void generateBeanNameWithDefaultNamedComponent() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertNotNull("The generated beanName must *never* be null.", beanName);
	assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
	assertEquals("thoreau", beanName);
}
 
private void testDefault(ClassPathScanningCandidateComponentProvider provider,
		Class<? extends BeanDefinition> expectedBeanDefinitionType) {
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
	assertTrue(containsBeanClass(candidates, StubFooDao.class));
	assertTrue(containsBeanClass(candidates, NamedStubDao.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertEquals(7, candidates.size());
	assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
 
@Test
public void customSupportIncludeFilterWithNonIndexedTypeUseScan() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
	// This annotation type is not directly annotated with Indexed so we can use
	// the index to find candidates
	provider.addIncludeFilter(new AnnotationTypeFilter(CustomStereotype.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class));
	assertEquals(1, candidates.size());
	assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class);
}
 
@Test
public void withMultipleAnnotationIncludeFilters1() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters1.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class);
	ctx.refresh();
	ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated
	ctx.getBean(MessageBean.class);           // @CustomComponent-annotated
}
 
@Test
public void generateBeanNameWithDefaultNamedComponent() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertNotNull("The generated beanName must *never* be null.", beanName);
	assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
	assertEquals("thoreau", beanName);
}