org.springframework.core.type.filter.AnnotationTypeFilter Java Examples

The following examples show how to use org.springframework.core.type.filter.AnnotationTypeFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Neo4jConfigurationSupport.java    From sdn-rx with Apache License 2.0 7 votes vote down vote up
/**
 * Scans the given base package for entities, i.e. Neo4j specific types annotated with {@link Node}.
 *
 * @param basePackage must not be {@literal null}.
 * @return found entities in the package to scan.
 * @throws ClassNotFoundException if the given class cannot be loaded by the class loader.
 */
protected final Set<Class<?>> scanForEntities(String basePackage) throws ClassNotFoundException {

	if (!StringUtils.hasText(basePackage)) {
		return Collections.emptySet();
	}

	Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();

	ClassPathScanningCandidateComponentProvider componentProvider =
		new ClassPathScanningCandidateComponentProvider(false);
	componentProvider.addIncludeFilter(new AnnotationTypeFilter(Node.class));

	ClassLoader classLoader = Neo4jConfigurationSupport.class.getClassLoader();
	for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
		initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), classLoader));
	}

	return initialEntitySet;
}
 
Example #2
Source File: SpringControllerResolver.java    From RestDoc with Apache License 2.0 7 votes vote down vote up
@Override
public List<Class> getClasses() {
    List<String> packages = _packages;

    var scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));

    var classes = new ArrayList<Class>();
    if (packages == null) packages = Arrays.asList("cn", "com");
    for (var packageName : packages) {
        var beans = scanner.findCandidateComponents(packageName);
        for (var bean : beans) {
            try {
                var className = bean.getBeanClassName();
                Class clazz = Class.forName(className);

                classes.add(clazz);
            } catch (ClassNotFoundException e) {
                _logger.warn("not found class:" + bean.getBeanClassName(), e);
            }
        }
    }
    return classes;
}
 
Example #3
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@Test
public void testWithComponentAnnotationOnly() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Controller.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(3, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertFalse(containsBeanClass(candidates, FooServiceImpl.class));
	assertFalse(containsBeanClass(candidates, StubFooDao.class));
	assertFalse(containsBeanClass(candidates, NamedStubDao.class));
}
 
Example #4
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationExcludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(11, beanCount);
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
 
Example #5
Source File: NetworkAssemble.java    From network-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	ClassPathScanningCandidateComponentProvider scanner = new ScanningComponent(Boolean.FALSE, this.environment);
	scanner.setResourceLoader(this.resourceLoader);

	AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(Network.class);
	scanner.addIncludeFilter(annotationTypeFilter);

	String packageName = ClassUtils.getPackageName(importingClassMetadata.getClassName());
	Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(packageName);
	candidateComponents.forEach(beanDefinition -> {
		AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
		AnnotationMetadata annotationMetadata = annotatedBeanDefinition.getMetadata();
		BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(NetworkFactory.class);
		String className = annotationMetadata.getClassName();
		definition.addPropertyValue(NetworkFactoryConstants.PROPERTY_VALUE.getValue(), className);
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

		AbstractBeanDefinition abstractBeanDefinition = definition.getBeanDefinition();
		BeanDefinitionHolder holder = new BeanDefinitionHolder(abstractBeanDefinition, className);
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);

	});

}
 
Example #6
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testWithComponentAnnotationOnly() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Controller.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(3, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertFalse(containsBeanClass(candidates, FooServiceImpl.class));
	assertFalse(containsBeanClass(candidates, StubFooDao.class));
	assertFalse(containsBeanClass(candidates, NamedStubDao.class));
}
 
Example #7
Source File: GRpcClientRegister.java    From faster-framework-project with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(GRpcClientScan.class.getCanonicalName()));
    if (annotationAttributes == null) {
        log.warn("GrpcScan was not found.Please check your configuration.");
        return;
    }
    ClassPathGRpcServiceScanner classPathGrpcServiceScanner = new ClassPathGRpcServiceScanner(registry, beanFactory);
    classPathGrpcServiceScanner.setResourceLoader(this.resourceLoader);
    classPathGrpcServiceScanner.addIncludeFilter(new AnnotationTypeFilter(GRpcService.class));
    List<String> basePackages = AutoConfigurationPackages.get(this.beanFactory);
    for (String pkg : annotationAttributes.getStringArray("basePackages")) {
        if (StringUtils.hasText(pkg)) {
            basePackages.add(pkg);
        }
    }
    classPathGrpcServiceScanner.doScan(StringUtils.toStringArray(basePackages));
}
 
Example #8
Source File: GRpcApiRegister.java    From faster-framework-project with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(GRpcServerScan.class.getCanonicalName()));
    if (annotationAttributes == null) {
        log.warn("GrpcScan was not found.Please check your configuration.");
        return;
    }
    ClassPathBeanDefinitionScanner classPathGrpcApiScanner = new ClassPathBeanDefinitionScanner(registry, false);
    classPathGrpcApiScanner.setResourceLoader(this.resourceLoader);
    classPathGrpcApiScanner.addIncludeFilter(new AnnotationTypeFilter(GRpcApi.class));
    List<String> basePackages = AutoConfigurationPackages.get(this.beanFactory);
    for (String pkg : annotationAttributes.getStringArray("basePackages")) {
        if (StringUtils.hasText(pkg)) {
            basePackages.add(pkg);
        }
    }
    classPathGrpcApiScanner.scan(StringUtils.toStringArray(basePackages));
}
 
Example #9
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);

	assertEquals(10, beanCount);
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #10
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);

	assertEquals(10, beanCount);
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #11
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomAnnotationExcludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(11, beanCount);
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
 
Example #12
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomIncludeFilterWithoutDefaultsAndNoPostProcessors() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(6, beanCount);
	assertTrue(context.containsBean("messageBean"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("stubFooDao"));
	assertFalse(context.containsBean("myNamedComponent"));
	assertFalse(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #13
Source File: NettyRpcClientBeanDefinitionRegistrar.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
    GenericBeanDefinition beanPostProcessorDefinition = new GenericBeanDefinition();
    beanPostProcessorDefinition.setInstanceSupplier(()->this);
    beanPostProcessorDefinition.setBeanClass(BeanPostProcessor.class);
    registry.registerBeanDefinition("NettyRpcClientBeanPostProcessor",beanPostProcessorDefinition);

    ClassPathScanningCandidateComponentProvider scanner = getScanner();
    scanner.setResourceLoader(resourceLoader);
    scanner.addIncludeFilter(new AnnotationTypeFilter(NettyRpcClient.class));
    Map<String, Object> enableNettyRpcClientsAttributes = metadata.getAnnotationAttributes(enableNettyRpcClientsCanonicalName);

    for (String basePackage : getBasePackages(metadata,enableNettyRpcClientsAttributes)) {
        for (BeanDefinition candidateComponent : scanner.findCandidateComponents(basePackage)) {
            if (!(candidateComponent instanceof AnnotatedBeanDefinition)) {
                continue;
            }

            AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
            if(!beanDefinition.getMetadata().isInterface()) {
                throw new IllegalArgumentException("@NettyRpcClient can only be specified on an interface");
            }
            registerNettyRpcClient(beanDefinition,registry);
        }
    }
}
 
Example #14
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomIncludeFilterWithoutDefaultsAndNoPostProcessors() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(6, beanCount);
	assertTrue(context.containsBean("messageBean"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("stubFooDao"));
	assertFalse(context.containsBean("myNamedComponent"));
	assertFalse(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #15
Source File: ClassPathBeanDefinitionScannerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomIncludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(13, beanCount);
	assertTrue(context.containsBean("messageBean"));
	assertTrue(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #16
Source File: SimpleClassScanner.java    From mercury with Apache License 2.0 6 votes vote down vote up
public List<Class<?>> getAnnotatedClasses(String scanPath, Class<? extends Annotation> type) {
    if (!scanPath.contains(".")) {
        throw new IllegalArgumentException(EX_START + scanPath + EX_END);
    }
    List<Class<?>> result = new ArrayList<>();
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AnnotationTypeFilter(type));
    for (BeanDefinition beanDef : provider.findCandidateComponents(scanPath)) {
        try {
            result.add(Class.forName(beanDef.getBeanClassName()));
        } catch (ClassNotFoundException e) {
            // ok to ignore
        }
    }
    return result;
}
 
Example #17
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomIncludeFilterAndDefaults() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(13, beanCount);
	assertTrue(context.containsBean("messageBean"));
	assertTrue(context.containsBean("serviceInvocationCounter"));
	assertTrue(context.containsBean("fooServiceImpl"));
	assertTrue(context.containsBean("stubFooDao"));
	assertTrue(context.containsBean("myNamedComponent"));
	assertTrue(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #18
Source File: SpringContainer.java    From dorado with Apache License 2.0 6 votes vote down vote up
public synchronized static void create(ApplicationContext applicationContext) {
	DoradoServerBuilder builder = Dorado.serverConfig;
	if (builder == null) {
		throw new IllegalStateException("Please init DoradoServer first!");
	}

	if (!(applicationContext instanceof DoradoApplicationContext)
			&& (applicationContext instanceof BeanDefinitionRegistry)) {
		ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(
				(BeanDefinitionRegistry) applicationContext);
		scanner.resetFilters(false);
		scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
		scanner.scan(builder.scanPackages());
	}

	instance = new SpringContainer(applicationContext);
	Dorado.springInitialized = true;
}
 
Example #19
Source File: AnnotationBeanDefinitionRegistryPostProcessor.java    From spring-context-support with Apache License 2.0 6 votes vote down vote up
private void registerBeanDefinitions(BeanDefinitionRegistry registry, String[] basePackages) {

        ExposingClassPathBeanDefinitionScanner scanner = new ExposingClassPathBeanDefinitionScanner(registry, false,
                getEnvironment(), getResourceLoader());

        BeanNameGenerator beanNameGenerator = resolveAnnotatedBeanNameGenerator(registry);
        // Set the BeanNameGenerator
        scanner.setBeanNameGenerator(beanNameGenerator);
        // Add the AnnotationTypeFilter for annotationTypes
        for (Class<? extends Annotation> supportedAnnotationType : getSupportedAnnotationTypes()) {
            scanner.addIncludeFilter(new AnnotationTypeFilter(supportedAnnotationType));
        }
        // Register the primary BeanDefinitions
        Map<String, AnnotatedBeanDefinition> primaryBeanDefinitions = registerPrimaryBeanDefinitions(scanner, basePackages);
        // Register the secondary BeanDefinitions
        registerSecondaryBeanDefinitions(scanner, primaryBeanDefinitions, basePackages);
    }
 
Example #20
Source File: RpcExporterRegister.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
private Collection<BeanDefinition> getCandidates(ResourceLoader resourceLoader) {
    ClassPathScanningCandidateComponentProvider scanner =
            new ClassPathScanningCandidateComponentProvider(false, environment);

    scanner.addIncludeFilter(new AnnotationTypeFilter(RpcExporter.class));
    scanner.setResourceLoader(resourceLoader);
    return AutoConfigurationPackages.get(beanFactory).stream()
            .flatMap(basePackage -> scanner.findCandidateComponents(basePackage).stream())
            .collect(Collectors.toSet());
}
 
Example #21
Source File: SecretRequestAdvice.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
private void registerExclude() {
    secretScanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
    secretScanner.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
    //根据扫描包获取所有的类
    List<String> scanPackages = secretProperties.getExcludePackages();
    if (!CollectionUtils.isEmpty(scanPackages)) {
        for (String scanPackage : scanPackages) {
            Set<BeanDefinition> beanDefinitions = secretScanner.findCandidateComponents(scanPackage);
            for (BeanDefinition beanDefinition : beanDefinitions) {
                excludeClassList.add(beanDefinition.getBeanClassName());
            }
        }
    }
}
 
Example #22
Source File: AnnotationTypeFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInheritedAnnotationFromInterfaceDoesNotMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeClassWithSomeComponentInterface";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
	// Must fail as annotation on interfaces should not be considered a match
	assertFalse(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #23
Source File: AnnotationTypeFilterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testDirectAnnotationMatch() throws Exception {
	MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	String classUnderTest = "org.springframework.core.type.AnnotationTypeFilterTests$SomeComponent";
	MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(classUnderTest);

	AnnotationTypeFilter filter = new AnnotationTypeFilter(InheritedAnnotation.class);
	assertTrue(filter.match(metadataReader, metadataReaderFactory));
	ClassloadingAssertions.assertClassNotLoaded(classUnderTest);
}
 
Example #24
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	assertEquals(6, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertFalse(containsBeanClass(candidates, FooServiceImpl.class));
}
 
Example #25
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
	assertEquals(7, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, FooServiceImpl.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
}
 
Example #26
Source File: ClassScaner.java    From oim-fx with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Set<Class> scan(String[] basePackages, Class<? extends Annotation>... annotations) {
	ClassScaner cs = new ClassScaner();
	for (Class anno : annotations) {
		cs.addIncludeFilter(new AnnotationTypeFilter(anno));
	}
	Set<Class> classes = new HashSet<Class>();
	for (String s : basePackages) {
		classes.addAll(cs.doScan(s));
	}
	return classes;
}
 
Example #27
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testWithAspectAnnotationOnly() throws Exception {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.addIncludeFilter(new AnnotationTypeFilter(Aspect.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertEquals(1, candidates.size());
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
}
 
Example #28
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example #29
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandidateComponentProvider provider,
		Class<? extends BeanDefinition> expectedBeanDefinitionType) {
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertEquals(3, candidates.size());
	assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
 
Example #30
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customFiltersFollowedByResetUseIndex() {
	ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
	provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER));
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.resetFilters(true);
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class);
}