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;
}
}
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;
}
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);
}
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);
}
}
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);
}
}
Aggregations