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.
}
}
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;
}
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");
}
Aggregations