Search in sources :

Example 1 with ClassPathBeanDefinitionScanner

use of cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner in project today-framework by TAKETODAY.

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("cn.taketoday.core.env.scan2");
    ctx.refresh();
    assertThat(scanner.getEnvironment()).isEqualTo(ctx.getEnvironment());
    assertThat(ctx.containsBean(Constants.DEV_BEAN_NAME)).isFalse();
    assertThat(ctx.containsBean(Constants.PROD_BEAN_NAME)).isTrue();
}
Also used : ClassPathBeanDefinitionScanner(cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Example 2 with ClassPathBeanDefinitionScanner

use of cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner in project today-framework by TAKETODAY.

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("cn.taketoday.core.env.scan1");
    ctx.refresh();
    assertThat(ctx.containsBean(Constants.DEV_BEAN_NAME)).isFalse();
    assertThat(ctx.containsBean(Constants.PROD_BEAN_NAME)).isTrue();
}
Also used : ClassPathBeanDefinitionScanner(cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Example 3 with ClassPathBeanDefinitionScanner

use of cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner in project today-framework by TAKETODAY.

the class ClassPathBeanDefinitionScannerScopeIntegrationTests method createContext.

private ApplicationContext createContext(ScopedProxyMode scopedProxyMode) {
    GenericWebServletApplicationContext context = new GenericWebServletApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setIncludeAnnotationConfig(false);
    scanner.setBeanNameGenerator((definition, registry) -> definition.getScope());
    scanner.setScopedProxyMode(scopedProxyMode);
    // Scan twice in order to find errors in the bean definition compatibility check.
    scanner.scan(getClass().getPackage().getName());
    scanner.scan(getClass().getPackage().getName());
    AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context);
    reader.register(AppConfig1.class);
    context.refresh();
    return context;
}
Also used : ClassPathBeanDefinitionScanner(cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader) GenericWebServletApplicationContext(cn.taketoday.web.context.support.GenericWebServletApplicationContext)

Example 4 with ClassPathBeanDefinitionScanner

use of cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner in project today-framework by TAKETODAY.

the class AnnotationConfigWebApplicationContext method loadBeanDefinitions.

/**
 * Register a {@link BeanDefinition} for
 * any classes specified by {@link #register(Class...)} and scan any packages
 * specified by {@link #scan(String...)}.
 * <p>For any values specified by {@link #setConfigLocation(String)} or
 * {@link #setConfigLocations(String[])}, attempt first to load each location as a
 * class, registering a {@code BeanDefinition} if class loading is successful,
 * and if class loading fails (i.e. a {@code ClassNotFoundException} is raised),
 * assume the value is a package and attempt to scan it for component classes.
 * <p>Enables the default set of annotation configuration post processors, such that
 * {@code @Autowired}, {@code @Required}, and associated annotations can be used.
 * <p>Configuration class bean definitions are registered with generated bean
 * definition names unless the {@code value} attribute is provided to the stereotype
 * annotation.
 *
 * @param beanFactory the bean factory to load bean definitions into
 * @see #register(Class...)
 * @see #scan(String...)
 * @see #setConfigLocation(String)
 * @see #setConfigLocations(String[])
 * @see AnnotatedBeanDefinitionReader
 * @see ClassPathBeanDefinitionScanner
 */
@Override
protected void loadBeanDefinitions(StandardBeanFactory beanFactory) {
    AnnotatedBeanDefinitionReader reader = getAnnotatedBeanDefinitionReader(beanFactory);
    ClassPathBeanDefinitionScanner scanner = getClassPathBeanDefinitionScanner(beanFactory);
    BeanNameGenerator nameGenerator = getBeanNameGenerator();
    if (nameGenerator != null) {
        reader.setBeanNameGenerator(nameGenerator);
        scanner.setBeanNameGenerator(nameGenerator);
        beanFactory.registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, nameGenerator);
    }
    ScopeMetadataResolver scopeMetadataResolver = getScopeMetadataResolver();
    if (scopeMetadataResolver != null) {
        reader.setScopeMetadataResolver(scopeMetadataResolver);
        scanner.setScopeMetadataResolver(scopeMetadataResolver);
    }
    if (!componentClasses.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Registering component classes: [{}]", StringUtils.collectionToCommaDelimitedString(componentClasses));
        }
        reader.register(ClassUtils.toClassArray(componentClasses));
    }
    if (!basePackages.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Scanning base packages: [{}]", StringUtils.collectionToCommaDelimitedString(basePackages));
        }
        scanner.scan(StringUtils.toStringArray(this.basePackages));
    }
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        for (String configLocation : configLocations) {
            try {
                Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader());
                if (log.isTraceEnabled()) {
                    log.trace("Registering [{}]", configLocation);
                }
                reader.register(clazz);
            } catch (ClassNotFoundException ex) {
                if (log.isTraceEnabled()) {
                    log.trace("Could not load class for config location [{}] - trying package scan. {}", configLocation, ex.toString());
                }
                int count = scanner.scan(configLocation);
                if (count == 0 && log.isDebugEnabled()) {
                    log.debug("No component classes found for specified class/package [{}]", configLocation);
                }
            }
        }
    }
}
Also used : ClassPathBeanDefinitionScanner(cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner) BeanNameGenerator(cn.taketoday.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(cn.taketoday.context.annotation.AnnotationBeanNameGenerator) ScopeMetadataResolver(cn.taketoday.context.loader.ScopeMetadataResolver) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)

Example 5 with ClassPathBeanDefinitionScanner

use of cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner in project today-infrastructure by TAKETODAY.

the class AnnotationConfigWebApplicationContext method loadBeanDefinitions.

/**
 * Register a {@link BeanDefinition} for
 * any classes specified by {@link #register(Class...)} and scan any packages
 * specified by {@link #scan(String...)}.
 * <p>For any values specified by {@link #setConfigLocation(String)} or
 * {@link #setConfigLocations(String[])}, attempt first to load each location as a
 * class, registering a {@code BeanDefinition} if class loading is successful,
 * and if class loading fails (i.e. a {@code ClassNotFoundException} is raised),
 * assume the value is a package and attempt to scan it for component classes.
 * <p>Enables the default set of annotation configuration post processors, such that
 * {@code @Autowired}, {@code @Required}, and associated annotations can be used.
 * <p>Configuration class bean definitions are registered with generated bean
 * definition names unless the {@code value} attribute is provided to the stereotype
 * annotation.
 *
 * @param beanFactory the bean factory to load bean definitions into
 * @see #register(Class...)
 * @see #scan(String...)
 * @see #setConfigLocation(String)
 * @see #setConfigLocations(String[])
 * @see AnnotatedBeanDefinitionReader
 * @see ClassPathBeanDefinitionScanner
 */
@Override
protected void loadBeanDefinitions(StandardBeanFactory beanFactory) {
    AnnotatedBeanDefinitionReader reader = getAnnotatedBeanDefinitionReader(beanFactory);
    ClassPathBeanDefinitionScanner scanner = getClassPathBeanDefinitionScanner(beanFactory);
    BeanNameGenerator nameGenerator = getBeanNameGenerator();
    if (nameGenerator != null) {
        reader.setBeanNameGenerator(nameGenerator);
        scanner.setBeanNameGenerator(nameGenerator);
        beanFactory.registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, nameGenerator);
    }
    ScopeMetadataResolver scopeMetadataResolver = getScopeMetadataResolver();
    if (scopeMetadataResolver != null) {
        reader.setScopeMetadataResolver(scopeMetadataResolver);
        scanner.setScopeMetadataResolver(scopeMetadataResolver);
    }
    if (!componentClasses.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Registering component classes: [{}]", StringUtils.collectionToCommaDelimitedString(componentClasses));
        }
        reader.register(ClassUtils.toClassArray(componentClasses));
    }
    if (!basePackages.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("Scanning base packages: [{}]", StringUtils.collectionToCommaDelimitedString(basePackages));
        }
        scanner.scan(StringUtils.toStringArray(this.basePackages));
    }
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        for (String configLocation : configLocations) {
            try {
                Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader());
                if (log.isTraceEnabled()) {
                    log.trace("Registering [{}]", configLocation);
                }
                reader.register(clazz);
            } catch (ClassNotFoundException ex) {
                if (log.isTraceEnabled()) {
                    log.trace("Could not load class for config location [{}] - trying package scan. {}", configLocation, ex.toString());
                }
                int count = scanner.scan(configLocation);
                if (count == 0 && log.isDebugEnabled()) {
                    log.debug("No component classes found for specified class/package [{}]", configLocation);
                }
            }
        }
    }
}
Also used : ClassPathBeanDefinitionScanner(cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner) BeanNameGenerator(cn.taketoday.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(cn.taketoday.context.annotation.AnnotationBeanNameGenerator) ScopeMetadataResolver(cn.taketoday.context.loader.ScopeMetadataResolver) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)

Aggregations

ClassPathBeanDefinitionScanner (cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner)8 AnnotatedBeanDefinitionReader (cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)4 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)4 Test (org.junit.jupiter.api.Test)4 BeanNameGenerator (cn.taketoday.beans.factory.support.BeanNameGenerator)2 AnnotationBeanNameGenerator (cn.taketoday.context.annotation.AnnotationBeanNameGenerator)2 ScopeMetadataResolver (cn.taketoday.context.loader.ScopeMetadataResolver)2 GenericWebServletApplicationContext (cn.taketoday.web.context.support.GenericWebServletApplicationContext)2