Example usage for org.springframework.beans.factory.config BeanDefinition setPrimary

List of usage examples for org.springframework.beans.factory.config BeanDefinition setPrimary

Introduction

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

Prototype

void setPrimary(boolean primary);

Source Link

Document

Set whether this bean is a primary autowire candidate.

Usage

From source file:com.github.ljtfreitas.restify.spring.configure.RestifyProxyBeanBuilder.java

public BeanDefinition build() {
    BeanDefinition beanDefinition = doBuild();
    beanDefinition.setPrimary(true);
    return beanDefinition;
}

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 {//from w w  w  .  ja  va  2s  .  c  o m
        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());
}