Search in sources :

Example 31 with AssignableTypeFilter

use of org.springframework.core.type.filter.AssignableTypeFilter in project spring-framework by spring-projects.

the class ClassPathScanningCandidateComponentProviderTests method testWithMultipleMatchingFilters.

@Test
public void testWithMultipleMatchingFilters() {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
    provider.addIncludeFilter(new AssignableTypeFilter(FooServiceImpl.class));
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
    assertThat(candidates.size()).isEqualTo(7);
    assertThat(containsBeanClass(candidates, NamedComponent.class)).isTrue();
    assertThat(containsBeanClass(candidates, ServiceInvocationCounter.class)).isTrue();
    assertThat(containsBeanClass(candidates, FooServiceImpl.class)).isTrue();
    assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ProfileMetaAnnotatedComponent(example.profilescan.ProfileMetaAnnotatedComponent) NamedComponent(example.scannable.NamedComponent) AnnotatedComponent(example.gh24375.AnnotatedComponent) ProfileAnnotatedComponent(example.profilescan.ProfileAnnotatedComponent) BarComponent(example.scannable.sub.BarComponent) DevComponent(example.profilescan.DevComponent) Component(org.springframework.stereotype.Component) DefaultNamedComponent(example.scannable.DefaultNamedComponent) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.jupiter.api.Test)

Example 32 with AssignableTypeFilter

use of org.springframework.core.type.filter.AssignableTypeFilter in project spring-framework by spring-projects.

the class ClassPathScanningCandidateComponentProviderTests method testExcludeTakesPrecedence.

@Test
public void testExcludeTakesPrecedence() {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
    provider.addIncludeFilter(new AssignableTypeFilter(FooServiceImpl.class));
    provider.addExcludeFilter(new AssignableTypeFilter(FooService.class));
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
    assertThat(candidates.size()).isEqualTo(6);
    assertThat(containsBeanClass(candidates, NamedComponent.class)).isTrue();
    assertThat(containsBeanClass(candidates, ServiceInvocationCounter.class)).isTrue();
    assertThat(containsBeanClass(candidates, BarComponent.class)).isTrue();
    assertThat(containsBeanClass(candidates, FooServiceImpl.class)).isFalse();
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) AutowiredQualifierFooService(example.scannable.AutowiredQualifierFooService) FooService(example.scannable.FooService) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) ProfileMetaAnnotatedComponent(example.profilescan.ProfileMetaAnnotatedComponent) NamedComponent(example.scannable.NamedComponent) AnnotatedComponent(example.gh24375.AnnotatedComponent) ProfileAnnotatedComponent(example.profilescan.ProfileAnnotatedComponent) BarComponent(example.scannable.sub.BarComponent) DevComponent(example.profilescan.DevComponent) Component(org.springframework.stereotype.Component) DefaultNamedComponent(example.scannable.DefaultNamedComponent) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.jupiter.api.Test)

Example 33 with AssignableTypeFilter

use of org.springframework.core.type.filter.AssignableTypeFilter in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testMultipleCustomExcludeFiltersAndDefaults.

@Test
public void testMultipleCustomExcludeFiltersAndDefaults() {
    GenericApplicationContext context = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
    scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class));
    scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
    int beanCount = scanner.scan(BASE_PACKAGE);
    assertThat(beanCount).isGreaterThanOrEqualTo(10);
    assertThat(context.containsBean("fooServiceImpl")).isFalse();
    assertThat(context.containsBean("serviceInvocationCounter")).isFalse();
    assertThat(context.containsBean("stubFooDao")).isTrue();
    assertThat(context.containsBean("myNamedComponent")).isTrue();
    assertThat(context.containsBean("myNamedDao")).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)).isTrue();
}
Also used : FooService(example.scannable.FooService) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Aspect(org.aspectj.lang.annotation.Aspect) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.jupiter.api.Test)

Example 34 with AssignableTypeFilter

use of org.springframework.core.type.filter.AssignableTypeFilter in project spring-framework by spring-projects.

the class AssignableTypeFilterTests method superClassMatch.

@Test
void superClassMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "example.type.AssignableTypeFilterTestsTypes$SomeDaoLikeImpl";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AssignableTypeFilter filter = new AssignableTypeFilter(example.type.AssignableTypeFilterTestsTypes.SimpleJdbcDaoSupport.class);
    assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue();
    ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.jupiter.api.Test)

Example 35 with AssignableTypeFilter

use of org.springframework.core.type.filter.AssignableTypeFilter in project spring-framework by spring-projects.

the class AssignableTypeFilterTests method directMatch.

@Test
void directMatch() throws Exception {
    MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
    String classUnderTest = "example.type.AssignableTypeFilterTestsTypes$TestNonInheritingClass";
    MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);
    AssignableTypeFilter matchingFilter = new AssignableTypeFilter(example.type.AssignableTypeFilterTestsTypes.TestNonInheritingClass.class);
    AssignableTypeFilter notMatchingFilter = new AssignableTypeFilter(example.type.AssignableTypeFilterTestsTypes.TestInterface.class);
    assertThat(notMatchingFilter.match(metadataReader, metadataReaderFactory)).isFalse();
    assertThat(matchingFilter.match(metadataReader, metadataReaderFactory)).isTrue();
}
Also used : MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) SimpleMetadataReaderFactory(org.springframework.core.type.classreading.SimpleMetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) Test(org.junit.jupiter.api.Test)

Aggregations

AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)36 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)20 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)14 Test (org.junit.jupiter.api.Test)13 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)9 FooService (example.scannable.FooService)6 Annotation (java.lang.annotation.Annotation)5 ArrayList (java.util.ArrayList)4 MetadataReader (org.springframework.core.type.classreading.MetadataReader)4 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)4 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)4 AspectJTypeFilter (org.springframework.core.type.filter.AspectJTypeFilter)4 RegexPatternTypeFilter (org.springframework.core.type.filter.RegexPatternTypeFilter)4 TypeFilter (org.springframework.core.type.filter.TypeFilter)4 AutowiredQualifierFooService (example.scannable.AutowiredQualifierFooService)3 FooServiceImpl (example.scannable.FooServiceImpl)3 Test (org.junit.Test)3 AnnotatedComponent (example.gh24375.AnnotatedComponent)2 DevComponent (example.profilescan.DevComponent)2 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)2