Search in sources :

Example 1 with ClassPathBeanDefinitionScanner

use of org.springframework.context.annotation.ClassPathBeanDefinitionScanner in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method createContext.

private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setIncludeAnnotationConfig(false);
    scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {

        @Override
        public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
            ScopeMetadata metadata = new ScopeMetadata();
            if (definition instanceof AnnotatedBeanDefinition) {
                AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
                for (String type : annDef.getMetadata().getAnnotationTypes()) {
                    if (type.equals(javax.inject.Singleton.class.getName())) {
                        metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
                        break;
                    } else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
                        metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
                        metadata.setScopedProxyMode(scopedProxyMode);
                        break;
                    } else if (type.startsWith("javax.inject")) {
                        metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
                    }
                }
            }
            return metadata;
        }
    });
    // Scan twice in order to find errors in the bean definition compatibility check.
    scanner.scan(getClass().getPackage().getName());
    scanner.scan(getClass().getPackage().getName());
    context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
    context.refresh();
    return context;
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) ScopeMetadata(org.springframework.context.annotation.ScopeMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 2 with ClassPathBeanDefinitionScanner

use of org.springframework.context.annotation.ClassPathBeanDefinitionScanner in project data-prep by Talend.

the class ActionsImport method registerBeanDefinitions.

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    // Create the annotation-based context
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
    AtomicInteger actionCount = new AtomicInteger(0);
    scanner.setBeanNameGenerator((definition, beanRegistry) -> {
        try {
            final Class<?> clazz = Class.forName(definition.getBeanClassName());
            return ACTION_BEAN_PREFIX + AnnotationUtils.findAnnotation(clazz, Action.class).value();
        } catch (Exception e) {
            // Rather unexpected, filter must have found and check the class earlier.
            throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
        } finally {
            actionCount.incrementAndGet();
        }
    });
    scanner.addIncludeFilter(new ActionFilter());
    scanner.scan("org.talend.dataprep");
    // Import scanned services in current registry
    final String[] names = context.getBeanDefinitionNames();
    for (String name : names) {
        final BeanDefinition definition = context.getBeanDefinition(name);
        registry.registerBeanDefinition(name, definition);
    }
    LOGGER.info("{} action(s) found.", actionCount.get());
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) TDPException(org.talend.dataprep.exception.TDPException) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException)

Example 3 with ClassPathBeanDefinitionScanner

use of org.springframework.context.annotation.ClassPathBeanDefinitionScanner in project sandbox by irof.

the class CustomScanProcessor method postProcessBeanDefinitionRegistry.

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
    scanner.scan("factory.bean");
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner)

Example 4 with ClassPathBeanDefinitionScanner

use of org.springframework.context.annotation.ClassPathBeanDefinitionScanner in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents.

@Test
void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedComponents() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.setEnvironment(prodEnv);
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
    scanner.scan("org.springframework.core.env.scan2");
    ctx.refresh();
    assertThat(scanner.getEnvironment()).isEqualTo(ctx.getEnvironment());
    assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse();
    assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue();
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with ClassPathBeanDefinitionScanner

use of org.springframework.context.annotation.ClassPathBeanDefinitionScanner in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedConfigClasses.

@Test
void classPathBeanDefinitionScanner_inheritsEnvironmentFromEnvironmentCapableBDR_scanProfileAnnotatedConfigClasses() {
    // it's actually ConfigurationClassPostProcessor's Environment that gets the job done here.
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.setEnvironment(prodEnv);
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx);
    scanner.scan("org.springframework.core.env.scan1");
    ctx.refresh();
    assertThat(ctx.containsBean(DEV_BEAN_NAME)).isFalse();
    assertThat(ctx.containsBean(PROD_BEAN_NAME)).isTrue();
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

ClassPathBeanDefinitionScanner (org.springframework.context.annotation.ClassPathBeanDefinitionScanner)10 Test (org.junit.jupiter.api.Test)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ScopeMetadataResolver (org.springframework.context.annotation.ScopeMetadataResolver)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)2 GrailsPluginManager (grails.plugins.GrailsPluginManager)1 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 IOException (java.io.IOException)1 URL (java.net.URL)1 LinkedHashSet (java.util.LinkedHashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 HierarchicalBeanFactory (org.springframework.beans.factory.HierarchicalBeanFactory)1 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 AnnotatedBeanDefinitionReader (org.springframework.context.annotation.AnnotatedBeanDefinitionReader)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ScopeMetadata (org.springframework.context.annotation.ScopeMetadata)1