Java Code Examples for org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#setResourceLoader()

The following examples show how to use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider#setResourceLoader() . 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: 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 2
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 3
Source File: AnnotationMetadataHelper.java    From onetwo with Apache License 2.0 6 votes vote down vote up
public List<BeanDefinition> scanBeanDefinitions(Class<? extends Annotation> annoClass, String...extraPackagesToScans){
	ClassPathScanningCandidateComponentProvider scanner = createAnnotationScanner(classLoader, annoClass);
	if(resourceLoader!=null){
		scanner.setResourceLoader(resourceLoader);
	}
	/*Set<String> basePackages = getBasePackages();
	for (String basePackage : basePackages) {
		Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage);
		for (BeanDefinition candidateComponent : candidateComponents) {
			consumer.accept(candidateComponent);
		}
	}*/
	Set<String> basePackages = getBasePackages();
	if(!LangUtils.isEmpty(extraPackagesToScans)){
		basePackages.addAll(Arrays.asList(extraPackagesToScans));
	}
	return basePackages.stream()
						.flatMap(pack->scanner.findCandidateComponents(pack).stream())
						.collect(Collectors.toList());
}
 
Example 4
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 5
Source File: ExternalSources.java    From teiid-spring-boot with Apache License 2.0 5 votes vote down vote up
public void loadConnctionFactories(ClassLoader classloader, String packageName) {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.setResourceLoader(new ResourceLoader() {
        @Override
        public org.springframework.core.io.Resource getResource(String location) {
            return null;
        }
        @Override
        public ClassLoader getClassLoader() {
            return classloader;
        }
    });
    provider.addIncludeFilter(new AnnotationTypeFilter(ConnectionFactoryConfiguration.class));
    Set<BeanDefinition> components = provider.findCandidateComponents(packageName);
    for (BeanDefinition c : components) {
        try {
            Class<?> clazz = Class.forName(c.getBeanClassName(), false, classloader);
            ConnectionFactoryConfiguration cfc = clazz.getAnnotation(ConnectionFactoryConfiguration.class);
            if(cfc != null) {
                ExternalSource source = build(cfc, clazz);
                for (String name : cfc.otherAliases()) {
                    items.put(name, source);
                }
                items.put(source.getName(), source);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("failed to load " + c.getBeanClassName());
        }
    }
}
 
Example 6
Source File: StoreFragmentDetector.java    From spring-content with Apache License 2.0 5 votes vote down vote up
private Set<BeanDefinition> findCandidateBeanDefinitions() {
		ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false, environment);
		provider.setResourceLoader(resourceLoader);
		provider.setResourcePattern(format(CUSTOM_IMPLEMENTATION_RESOURCE_PATTERN, postfix));
		provider.setMetadataReaderFactory(metadataReaderFactory);
		provider.addIncludeFilter((reader, factory) -> true);

//		config.getExcludeFilters().forEach(it -> provider.addExcludeFilter(it));

		return basePackages.stream()//
				.flatMap(it -> provider.findCandidateComponents(it).stream())//
				.collect(Collectors.toSet());
	}
 
Example 7
Source File: CmisRegistrar.java    From spring-content with Apache License 2.0 5 votes vote down vote up
void cmisDocumentScan(String... basePackages) {
	ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CmisDocument.class));

	for (String basePackage : basePackages) {
		for (BeanDefinition bd : scanner.findCandidateComponents(basePackage)) {

			cmisDocumentBeanDefinition = bd;

			CmisEntityRepositoryComponentProvider cmisRepoScanner = new CmisEntityRepositoryComponentProvider(bd.getBeanClassName(), classLoader);
			scanner.setResourceLoader(resourceLoader);
			scanner.setEnvironment(environment);
			for (int i =0; i < basePackages.length && cmisDocumentRepositoryBeanDefinition == null; i++) {
				cmisDocumentRepositoryBeanDefinition = cmisRepoScanner.findCandidateComponents(basePackages[i])
						.stream()
						.findFirst()
						.get();
			}

			CmisEntityStorageComponentProvider cmisStoreScanner = new CmisEntityStorageComponentProvider(bd.getBeanClassName(), classLoader);
			scanner.setResourceLoader(resourceLoader);
			scanner.setEnvironment(environment);
			for (int i =0; i < basePackages.length && cmisDocumentStoreBeanDefinition == null; i++) {
				cmisDocumentStoreBeanDefinition = cmisStoreScanner.findCandidateComponents(basePackages[i])
					.stream()
					.findFirst()
					.get();
			}
		}
	}
}
 
Example 8
Source File: CmisRegistrar.java    From spring-content with Apache License 2.0 5 votes vote down vote up
void cmisFolderScan(String... basePackages) {

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

		for (String basePackage : basePackages) {

			for (BeanDefinition bd : scanner.findCandidateComponents(basePackage)) {

				if (bd.getBeanClassName().equals(CmisServiceBridge.Root.class.getName())) {
					continue;
				}

				cmisFolderBeanDefinition = bd;

				CmisEntityRepositoryComponentProvider cmisRepoScanner = new CmisEntityRepositoryComponentProvider(bd.getBeanClassName(), classLoader);
				scanner.setResourceLoader(resourceLoader);
				scanner.setEnvironment(environment);
				for (int i =0; i < basePackages.length && cmisFolderRepositoryBeanDefinition == null; i++) {
					cmisFolderRepositoryBeanDefinition = cmisRepoScanner.findCandidateComponents(basePackages[i])
							.stream()
							.findFirst()
							.get();
				}
			}
		}
	}
 
Example 9
Source File: Java110ListenerDiscoveryRegistrar.java    From MicroCommunity with Apache License 2.0 4 votes vote down vote up
/**
 * 注册侦听
 * @param metadata
 * @param registry
 */
public void registerListener(AnnotationMetadata metadata,
                             BeanDefinitionRegistry registry) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    ClassPathScanningCandidateComponentProvider scanner = getScanner();
    scanner.setResourceLoader(this.resourceLoader);
    Set<String> basePackages;
    Map<String, Object> attrs = metadata
            .getAnnotationAttributes(Java110ListenerDiscovery.class.getName());

    Object listenerPublishClassObj =  attrs.get("listenerPublishClass");

    Assert.notNull(listenerPublishClassObj,"Java110ListenerDiscovery 没有配置 listenerPublishClass 属性");

    Class<?> listenerPublishClass = (Class<?>) listenerPublishClassObj;

    AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(
            Java110Listener.class);

    scanner.addIncludeFilter(annotationTypeFilter);
    basePackages = getBasePackages(metadata);

    for (String basePackage : basePackages) {
        Set<BeanDefinition> candidateComponents = scanner
                .findCandidateComponents(basePackage);
        for (BeanDefinition candidateComponent : candidateComponents) {
            if (candidateComponent instanceof AnnotatedBeanDefinition) {
                // verify annotated class is an interface
                AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
                AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();


                Map<String, Object> attributes = annotationMetadata
                        .getAnnotationAttributes(
                                Java110Listener.class.getCanonicalName());

                String beanName = getListenerName(attributes,beanDefinition);

                /*BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(beanDefinition, beanName);
                BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);*/
                Method method = listenerPublishClass.getMethod("addListener",String.class);
                method.invoke(null,beanName);
            }
        }
    }
}
 
Example 10
Source File: ClassCandidateScanner.java    From wicket-spring-boot with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void postConstruct() {
	ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
			false);
	scanner.setEnvironment(this.environment);
	scanner.setResourceLoader(this.resourceLoader);
	scanner.addIncludeFilter(new AnnotationTypeFilter(SpringBootApplication.class));
	scanner.addIncludeFilter(new AnnotationTypeFilter(WicketHomePage.class));
	scanner.addIncludeFilter(new AnnotationTypeFilter(WicketSignInPage.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(WicketAccessDeniedPage.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(WicketExpiredPage.class));
    scanner.addIncludeFilter(new AnnotationTypeFilter(WicketInternalErrorPage.class));
    for (String basePackage : getMappingBasePackages(beanFactory)) {
		if (StringUtils.hasText(basePackage)) {
			pageCandidates().getBasePackages().add(basePackage);
			Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents(basePackage);
			for (BeanDefinition beanDefinition : beanDefinitions) {
				Class<?> beanClass;
				try {
					beanClass = ClassUtils.forName(beanDefinition.getBeanClassName(), classLoader);
				} catch (ClassNotFoundException e) {
					throw new IllegalStateException(e);
				}
				if(beanClass.isAnnotationPresent(WicketHomePage.class)){
					pageCandidates().getHomePageCandidates().add(new WicketClassCandidate<Page>((Class<Page>) beanClass));
				}
				if(beanClass.isAnnotationPresent(WicketSignInPage.class)){
					pageCandidates().getSignInPageCandidates().add(new WicketClassCandidate<WebPage>((Class<WebPage>) beanClass));
				}
				if(beanClass.isAnnotationPresent(WicketAccessDeniedPage.class)){
					pageCandidates().getAccessDeniedPageCandidates().add(new WicketClassCandidate<Page>((Class<Page>) beanClass));
				}
				if(beanClass.isAnnotationPresent(WicketExpiredPage.class)){
					pageCandidates().getExpiredPageCandidates().add(new WicketClassCandidate<Page>((Class<Page>) beanClass));
				}
				if(beanClass.isAnnotationPresent(WicketInternalErrorPage.class)){
					pageCandidates().getInternalErrorPageCandidates().add(new WicketClassCandidate<Page>((Class<Page>) beanClass));
				}
				if(beanClass.isAnnotationPresent(SpringBootApplication.class)){
					pageCandidates().setSpringBootMainClass( beanClass );
				}
			}
			
		}
    }
}