Search in sources :

Example 1 with BeanNamePopulator

use of cn.taketoday.beans.factory.support.BeanNamePopulator in project today-framework by TAKETODAY.

the class MapperScannerRegistrar method registerBeanDefinitions.

protected void registerBeanDefinitions(AnnotationMetadata annoMeta, MergedAnnotation<MapperScan> mapperScan, BeanDefinitionRegistry registry, String beanName) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
    builder.addPropertyValue("processPropertyPlaceHolders", true);
    Class<? extends Annotation> annotationClass = mapperScan.getClass("annotationClass");
    if (!Annotation.class.equals(annotationClass)) {
        builder.addPropertyValue("annotationClass", annotationClass);
    }
    Class<?> markerInterface = mapperScan.getClass("markerInterface");
    if (!Class.class.equals(markerInterface)) {
        builder.addPropertyValue("markerInterface", markerInterface);
    }
    Class<? extends BeanNamePopulator> generatorClass = mapperScan.getClass("namePopulator");
    if (!BeanNamePopulator.class.equals(generatorClass)) {
        builder.addPropertyValue("namePopulator", BeanUtils.newInstance(generatorClass));
    }
    Class<? extends MapperFactoryBean<?>> mapperFactoryBeanClass = mapperScan.getClass("factoryBean");
    if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
        builder.addPropertyValue("mapperFactoryBeanClass", mapperFactoryBeanClass);
    }
    String sqlSessionTemplateRef = mapperScan.getString("sqlSessionTemplateRef");
    if (StringUtils.hasText(sqlSessionTemplateRef)) {
        builder.addPropertyValue("sqlSessionTemplateBeanName", sqlSessionTemplateRef);
    }
    String sqlSessionFactoryRef = mapperScan.getString("sqlSessionFactoryRef");
    if (StringUtils.hasText(sqlSessionFactoryRef)) {
        builder.addPropertyValue("sqlSessionFactoryBeanName", sqlSessionFactoryRef);
    }
    ArrayList<String> basePackages = new ArrayList<>();
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("value")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getStringArray("basePackages")).filter(StringUtils::hasText).toList());
    basePackages.addAll(Arrays.stream(mapperScan.getClassArray("basePackageClasses")).map(ClassUtils::getPackageName).toList());
    if (basePackages.isEmpty()) {
        basePackages.add(getDefaultBasePackage(annoMeta));
    }
    String lazyInitialization = mapperScan.getString("lazyInitialization");
    if (StringUtils.hasText(lazyInitialization)) {
        builder.addPropertyValue("lazyInitialization", lazyInitialization);
    }
    String defaultScope = mapperScan.getString("defaultScope");
    if (StringUtils.hasText(defaultScope)) {
        builder.addPropertyValue("defaultScope", defaultScope);
    }
    builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages));
    builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    registry.registerBeanDefinition(beanName, builder.getRawBeanDefinition());
}
Also used : BeanNamePopulator(cn.taketoday.beans.factory.support.BeanNamePopulator) MapperFactoryBean(cn.taketoday.orm.mybatis.mapper.MapperFactoryBean) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) MergedAnnotation(cn.taketoday.core.annotation.MergedAnnotation) ClassUtils(cn.taketoday.util.ClassUtils) BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) StringUtils(cn.taketoday.util.StringUtils)

Example 2 with BeanNamePopulator

use of cn.taketoday.beans.factory.support.BeanNamePopulator 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);
    BeanNamePopulator namePopulator = getBeanNamePopulator();
    if (namePopulator != null) {
        reader.setBeanNamePopulator(namePopulator);
        scanner.setBeanNamePopulator(namePopulator);
        beanFactory.registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, namePopulator);
    }
    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) BeanNamePopulator(cn.taketoday.beans.factory.support.BeanNamePopulator) ScopeMetadataResolver(cn.taketoday.context.loader.ScopeMetadataResolver) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)

Aggregations

BeanNamePopulator (cn.taketoday.beans.factory.support.BeanNamePopulator)2 BeanDefinitionBuilder (cn.taketoday.beans.factory.support.BeanDefinitionBuilder)1 AnnotatedBeanDefinitionReader (cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)1 ClassPathBeanDefinitionScanner (cn.taketoday.context.annotation.ClassPathBeanDefinitionScanner)1 ScopeMetadataResolver (cn.taketoday.context.loader.ScopeMetadataResolver)1 MergedAnnotation (cn.taketoday.core.annotation.MergedAnnotation)1 MapperFactoryBean (cn.taketoday.orm.mybatis.mapper.MapperFactoryBean)1 ClassUtils (cn.taketoday.util.ClassUtils)1 StringUtils (cn.taketoday.util.StringUtils)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1