Example usage for org.springframework.beans.factory.config BeanDefinitionHolder getSource

List of usage examples for org.springframework.beans.factory.config BeanDefinitionHolder getSource

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config BeanDefinitionHolder getSource.

Prototype

@Override
@Nullable
public Object getSource() 

Source Link

Document

Expose the bean definition's source object.

Usage

From source file:org.jdal.aop.SerializableProxyUtils.java

public static BeanDefinitionHolder createSerializableProxy(BeanDefinitionHolder definition,
        BeanDefinitionRegistry registry, boolean proxyTargetClass) {

    String originalBeanName = definition.getBeanName();
    BeanDefinition targetDefinition = definition.getBeanDefinition();

    // Create a scoped proxy definition for the original bean name,
    // "hiding" the target bean in an internal target definition.
    RootBeanDefinition proxyDefinition = new RootBeanDefinition(SerializableProxyFactoryBean.class);
    proxyDefinition.setOriginatingBeanDefinition(definition.getBeanDefinition());
    proxyDefinition.setSource(definition.getSource());
    proxyDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

    String targetBeanName = getTargetBeanName(originalBeanName);
    proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);

    if (proxyTargetClass) {
        targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
    } else {/*  w w w .j a  va 2  s.c  om*/
        proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
    }

    // Copy autowire settings from original bean definition.
    proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
    proxyDefinition.setPrimary(targetDefinition.isPrimary());
    if (targetDefinition instanceof AbstractBeanDefinition) {
        proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
    }

    // Set singleton property of FactoryBean
    proxyDefinition.getPropertyValues().add("singleton", !targetDefinition.isPrototype());

    // The target bean should be ignored in favor of the scoped proxy.
    targetDefinition.setAutowireCandidate(false);
    targetDefinition.setPrimary(false);

    // Register the target bean as separate bean in the factory.
    registry.registerBeanDefinition(targetBeanName, targetDefinition);

    // Return the scoped proxy definition as primary bean definition
    // (potentially an inner bean).
    return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}