Search in sources :

Example 16 with AssignableTypeFilter

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

the class ClassPathScanningCandidateComponentProviderTests method testCustomAssignableTypeIncludeFilter.

private void testCustomAssignableTypeIncludeFilter(ClassPathScanningCandidateComponentProvider provider, Class<? extends BeanDefinition> expectedBeanDefinitionType) {
    provider.addIncludeFilter(new AssignableTypeFilter(FooService.class));
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
    // Interfaces/Abstract class are filtered out automatically.
    assertTrue(containsBeanClass(candidates, AutowiredQualifierFooService.class));
    assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
    assertTrue(containsBeanClass(candidates, ScopedProxyTestBean.class));
    assertEquals(3, candidates.size());
    assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
Also used : AutowiredQualifierFooService(example.scannable.AutowiredQualifierFooService) FooService(example.scannable.FooService) FooServiceImpl(example.scannable.FooServiceImpl) AutowiredQualifierFooService(example.scannable.AutowiredQualifierFooService) ScopedProxyTestBean(example.scannable.ScopedProxyTestBean) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) AnnotatedGenericBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 17 with AssignableTypeFilter

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

the class ComponentScanAnnotationParser method typeFiltersFor.

private List<TypeFilter> typeFiltersFor(AnnotationAttributes filterAttributes) {
    List<TypeFilter> typeFilters = new ArrayList<>();
    FilterType filterType = filterAttributes.getEnum("type");
    for (Class<?> filterClass : filterAttributes.getClassArray("classes")) {
        switch(filterType) {
            case ANNOTATION:
                Assert.isAssignable(Annotation.class, filterClass, "@ComponentScan ANNOTATION type filter requires an annotation type");
                @SuppressWarnings("unchecked") Class<Annotation> annotationType = (Class<Annotation>) filterClass;
                typeFilters.add(new AnnotationTypeFilter(annotationType));
                break;
            case ASSIGNABLE_TYPE:
                typeFilters.add(new AssignableTypeFilter(filterClass));
                break;
            case CUSTOM:
                Assert.isAssignable(TypeFilter.class, filterClass, "@ComponentScan CUSTOM type filter requires a TypeFilter implementation");
                TypeFilter filter = BeanUtils.instantiateClass(filterClass, TypeFilter.class);
                ParserStrategyUtils.invokeAwareMethods(filter, this.environment, this.resourceLoader, this.registry);
                typeFilters.add(filter);
                break;
            default:
                throw new IllegalArgumentException("Filter type not supported with Class value: " + filterType);
        }
    }
    for (String expression : filterAttributes.getStringArray("pattern")) {
        switch(filterType) {
            case ASPECTJ:
                typeFilters.add(new AspectJTypeFilter(expression, this.resourceLoader.getClassLoader()));
                break;
            case REGEX:
                typeFilters.add(new RegexPatternTypeFilter(Pattern.compile(expression)));
                break;
            default:
                throw new IllegalArgumentException("Filter type not supported with String pattern: " + filterType);
        }
    }
    return typeFilters;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) AspectJTypeFilter(org.springframework.core.type.filter.AspectJTypeFilter) ArrayList(java.util.ArrayList) TypeFilter(org.springframework.core.type.filter.TypeFilter) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) AspectJTypeFilter(org.springframework.core.type.filter.AspectJTypeFilter) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) RegexPatternTypeFilter(org.springframework.core.type.filter.RegexPatternTypeFilter) Annotation(java.lang.annotation.Annotation) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Aggregations

AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)17 Test (org.junit.Test)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)6 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)6 FooService (example.scannable.FooService)5 MetadataReader (org.springframework.core.type.classreading.MetadataReader)4 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)4 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)4 FooServiceImpl (example.scannable.FooServiceImpl)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 DevComponent (example.profilescan.DevComponent)2 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)2 ProfileMetaAnnotatedComponent (example.profilescan.ProfileMetaAnnotatedComponent)2 AutowiredQualifierFooService (example.scannable.AutowiredQualifierFooService)2 DefaultNamedComponent (example.scannable.DefaultNamedComponent)2 FooDao (example.scannable.FooDao)2 NamedComponent (example.scannable.NamedComponent)2 ServiceInvocationCounter (example.scannable.ServiceInvocationCounter)2 StubFooDao (example.scannable.StubFooDao)2