Search in sources :

Example 66 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project com.revolsys.open by revolsys.

the class ModuleImport method newTargetBeanDefinition.

public static GenericBeanDefinition newTargetBeanDefinition(final BeanDefinitionRegistry beanFactory, final String beanName) {
    if (beanFactory.containsBeanDefinition(beanName)) {
        final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        final boolean singleton = beanDefinition.isSingleton();
        final GenericBeanDefinition proxyBeanDefinition = new GenericBeanDefinition();
        proxyBeanDefinition.setBeanClass(TargetBeanFactoryBean.class);
        final MutablePropertyValues values = new MutablePropertyValues();
        final String beanClassName = beanDefinition.getBeanClassName();
        final PropertyValue beanDefinitionProperty = new PropertyValue("targetBeanDefinition", beanDefinition);
        beanDefinitionProperty.setConvertedValue(beanDefinition);
        values.addPropertyValue(beanDefinitionProperty);
        values.addPropertyValue("targetBeanName", beanName);
        values.addPropertyValue("targetBeanClass", beanClassName);
        values.addPropertyValue("targetBeanFactory", beanFactory);
        values.addPropertyValue("singleton", singleton);
        proxyBeanDefinition.setPropertyValues(values);
        return proxyBeanDefinition;
    } else {
        return null;
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 67 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project com.revolsys.open by revolsys.

the class ModuleImport method getApplicationContext.

protected GenericApplicationContext getApplicationContext(final BeanDefinitionRegistry parentRegistry) {
    if (this.applicationContext == null) {
        this.applicationContext = new GenericApplicationContext();
        if (parentRegistry instanceof ResourceLoader) {
            final ResourceLoader resourceLoader = (ResourceLoader) parentRegistry;
            final ClassLoader classLoader = resourceLoader.getClassLoader();
            this.applicationContext.setClassLoader(classLoader);
        }
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.applicationContext, null);
        final DefaultListableBeanFactory beanFactory = this.applicationContext.getDefaultListableBeanFactory();
        final BeanFactory parentBeanFactory = (BeanFactory) parentRegistry;
        for (final String beanName : parentRegistry.getBeanDefinitionNames()) {
            final BeanDefinition beanDefinition = parentRegistry.getBeanDefinition(beanName);
            final String beanClassName = beanDefinition.getBeanClassName();
            if (beanClassName != null) {
                if (beanClassName.equals(AttributeMap.class.getName())) {
                    registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                    this.beanNamesNotToExport.add(beanName);
                } else if (beanClassName.equals(MapFactoryBean.class.getName())) {
                    final PropertyValue targetMapClass = beanDefinition.getPropertyValues().getPropertyValue("targetMapClass");
                    if (targetMapClass != null) {
                        final Object mapClass = targetMapClass.getValue();
                        if (AttributeMap.class.getName().equals(mapClass)) {
                            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
                            this.beanNamesNotToExport.add(beanName);
                        }
                    }
                }
            }
        }
        beanFactory.addPropertyEditorRegistrar(this.resourceEditorRegistrar);
        final AttributesBeanConfigurer attributesConfig = new AttributesBeanConfigurer(this.applicationContext, this.parameters);
        this.applicationContext.addBeanFactoryPostProcessor(attributesConfig);
        for (final String beanName : this.importBeanNames) {
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, beanName);
            this.beanNamesNotToExport.add(beanName);
        }
        for (final Entry<String, String> entry : this.importBeanAliases.entrySet()) {
            final String beanName = entry.getKey();
            final String aliasName = entry.getValue();
            registerTargetBeanDefinition(this.applicationContext, parentBeanFactory, beanName, aliasName);
            this.beanNamesNotToExport.add(aliasName);
        }
        final XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(this.applicationContext);
        for (final Resource resource : this.resources) {
            beanReader.loadBeanDefinitions(resource);
        }
        this.applicationContext.refresh();
    }
    return this.applicationContext;
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) AttributesBeanConfigurer(com.revolsys.spring.config.AttributesBeanConfigurer) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Resource(com.revolsys.spring.resource.Resource) PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AttributeMap(com.revolsys.collection.map.AttributeMap) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)

Example 68 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project com.revolsys.open by revolsys.

the class AttributesBeanConfigurer method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final Map<String, Object> allAttributes = new LinkedHashMap<>();
    final Map<String, Object> threadAttributes = ThreadSharedProperties.getProperties();
    allAttributes.putAll(threadAttributes);
    processPlaceholderAttributes(beanFactory, threadAttributes);
    final Map<String, Object> attributes = getFields();
    processPlaceholderAttributes(beanFactory, attributes);
    for (final Entry<String, Object> entry : attributes.entrySet()) {
        final String key = entry.getKey();
        if (!allAttributes.containsKey(key)) {
            final Object value = entry.getValue();
            allAttributes.put(key, value);
        }
    }
    final String configBeanName = getBeanName();
    for (final String beanName : beanFactory.getBeanDefinitionNames()) {
        // locations.
        if (!beanName.equals(configBeanName)) {
            final BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
            final String beanClassName = bd.getBeanClassName();
            if (beanClassName != null) {
                addFields(allAttributes, beanFactory, bd, beanName, beanClassName);
                if (beanClassName.equals(TargetBeanFactoryBean.class.getName())) {
                    final MutablePropertyValues propertyValues = bd.getPropertyValues();
                    final BeanDefinition targetBeanDefinition = (BeanDefinition) propertyValues.getPropertyValue("targetBeanDefinition").getValue();
                    final String targetBeanClassName = targetBeanDefinition.getBeanClassName();
                    addFields(allAttributes, beanFactory, targetBeanDefinition, beanName, targetBeanClassName);
                }
            }
        }
    }
    processOverrideAttributes(beanFactory, allAttributes);
}
Also used : TargetBeanFactoryBean(com.revolsys.spring.TargetBeanFactoryBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) LinkedHashMap(java.util.LinkedHashMap)

Example 69 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project com.revolsys.open by revolsys.

the class BeanConfigurrer method setMapValue.

public void setMapValue(final ConfigurableListableBeanFactory factory, final String key, final String beanName, final String mapKey, final Object value) {
    final BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
    final String beanClassName = beanDefinition.getBeanClassName();
    try {
        final ClassLoader classLoader = this.applicationContext.getClassLoader();
        final Class<?> beanClass = Class.forName(beanClassName, true, classLoader);
        if (MapFactoryBean.class.isAssignableFrom(beanClass)) {
            final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
            final PropertyValue sourceMapProperty = propertyValues.getPropertyValue("sourceMap");
            @SuppressWarnings("unchecked") final Map<Object, Object> sourceMap = (Map<Object, Object>) sourceMapProperty.getValue();
            boolean found = false;
            for (final Entry<Object, Object> entry : sourceMap.entrySet()) {
                final Object mapEntryKey = entry.getKey();
                if (mapEntryKey instanceof TypedStringValue) {
                    final TypedStringValue typedKey = (TypedStringValue) mapEntryKey;
                    if (typedKey.getValue().equals(mapKey)) {
                        entry.setValue(value);
                        found = true;
                    }
                }
            }
            if (!found) {
                sourceMap.put(new TypedStringValue(mapKey), value);
            }
        } else if (!TargetBeanFactoryBean.class.isAssignableFrom(beanClass)) {
            Logs.error(this, "Bean class must be a MapFactoryBean, unable to set " + key + "=" + value);
        }
    } catch (final ClassNotFoundException e) {
        Logs.error(this, "Unable to set " + key + "=" + value, e);
    }
}
Also used : PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 70 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project com.revolsys.open by revolsys.

the class SetBeanProperties method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    for (final Entry<String, String> beanPropertyName : this.beanPropertyNames.entrySet()) {
        String beanName = beanPropertyName.getKey();
        final String[] aliases = beanFactory.getAliases(beanName);
        if (aliases.length > 0) {
            beanName = aliases[0];
        }
        final String propertyName = beanPropertyName.getValue();
        final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        beanDefinition.setLazyInit(false);
        final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
        propertyValues.add(propertyName, this.propertyValue);
    }
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)601 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)116 Test (org.junit.jupiter.api.Test)112 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)72 Test (org.junit.Test)66 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)53 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)41 ManagedList (org.springframework.beans.factory.support.ManagedList)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)35 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)34 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)34 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)31 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 Method (java.lang.reflect.Method)28 ArrayList (java.util.ArrayList)28 Map (java.util.Map)27