Search in sources :

Example 26 with AnnotationTypeFilter

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

the class ClassPathScanningCandidateComponentProvider method registerDefaultFilters.

/**
	 * Register the default filter for {@link Component @Component}.
	 * <p>This will implicitly register all annotations that have the
	 * {@link Component @Component} meta-annotation including the
	 * {@link Repository @Repository}, {@link Service @Service}, and
	 * {@link Controller @Controller} stereotype annotations.
	 * <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
	 * JSR-330's {@link javax.inject.Named} annotations, if available.
	 *
	 */
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
    this.includeFilters.add(new AnnotationTypeFilter(Component.class));
    ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
    try {
        this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
        logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
    } catch (ClassNotFoundException ex) {
    // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
    }
    try {
        this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
        logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
    } catch (ClassNotFoundException ex) {
    // JSR-330 API not available - simply skip.
    }
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) Component(org.springframework.stereotype.Component)

Example 27 with AnnotationTypeFilter

use of org.springframework.core.type.filter.AnnotationTypeFilter 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)

Example 28 with AnnotationTypeFilter

use of org.springframework.core.type.filter.AnnotationTypeFilter in project midpoint by Evolveum.

the class ConnectorFactoryBuiltinImpl method discoverConnectors.

private void discoverConnectors() {
    connectorMap = new HashMap<>();
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(ManagedConnector.class));
    LOGGER.trace("Scanning package {}", SCAN_PACKAGE);
    for (BeanDefinition bd : scanner.findCandidateComponents(SCAN_PACKAGE)) {
        LOGGER.debug("Found connector class {}", bd);
        String beanClassName = bd.getBeanClassName();
        try {
            Class connectorClass = Class.forName(beanClassName);
            ManagedConnector annotation = (ManagedConnector) connectorClass.getAnnotation(ManagedConnector.class);
            String type = annotation.type();
            LOGGER.debug("Found connector {} class {}", type, connectorClass);
            ConnectorStruct struct = createConnectorStruct(connectorClass, annotation);
            connectorMap.put(type, struct);
        } catch (ClassNotFoundException e) {
            LOGGER.error("Error loading connector class {}: {}", beanClassName, e.getMessage(), e);
        } catch (ObjectNotFoundException | SchemaException e) {
            LOGGER.error("Error discovering the connector {}: {}", beanClassName, e.getMessage(), e);
        }
    }
    LOGGER.trace("Scan done");
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ManagedConnector(com.evolveum.midpoint.provisioning.ucf.api.ManagedConnector) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)28 Test (org.junit.Test)17 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)11 DefaultNamedComponent (example.scannable.DefaultNamedComponent)7 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)7 Component (org.springframework.stereotype.Component)7 DevComponent (example.profilescan.DevComponent)6 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)6 ProfileMetaAnnotatedComponent (example.profilescan.ProfileMetaAnnotatedComponent)6 NamedComponent (example.scannable.NamedComponent)6 MetadataReader (org.springframework.core.type.classreading.MetadataReader)6 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)6 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)6 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)6 ServiceInvocationCounter (example.scannable.ServiceInvocationCounter)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 FooService (example.scannable.FooService)4 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)4 AutowiredQualifierFooService (example.scannable.AutowiredQualifierFooService)3 CustomComponent (example.scannable.CustomComponent)3