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

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

Introduction

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

Prototype

void setScope(@Nullable String scope);

Source Link

Document

Override the target scope of this bean, specifying a new scope name.

Usage

From source file:com.helpinput.spring.BeanRegister.java

public static BeanDefinitionWrap registerBean(ApplicationContext context, DefaultListableBeanFactory dlbf,
        BeanDefinitionBuilder builder, Class<?> clz, String beanName, String scope, BeanInfo beanInfo) {
    if (!Utils.hasLength(beanName)) {
        beanName = Commons.getBeanName(clz);
        if (!Utils.hasLength(beanName))
            beanName = Utils.beanName(beanInfo.scanName);
    }/*ww w  .ja  va 2  s  .  co m*/

    if (!Utils.hasLength(beanName))
        return null;

    //bean?bean
    if (beanName.endsWith("Impl")) {
        beanName = beanName.substring(0, beanName.length() - 4);
    }

    if (!Utils.hasLength(beanName))
        return null;

    removeBean(dlbf, beanInfo, null);

    BeanDefinition bd = null;
    if (!Utils.hasLength(scope))
        scope = findScopeStr(clz);

    List<BeanRegistInterceptor> interceptors = ContextHolder.beanRegistIntercpterHolder.getList();
    if (Utils.hasLength(interceptors)) {
        for (BeanRegistInterceptor beanRegistInterceptor : interceptors) {
            bd = beanRegistInterceptor.beforeRegist(clz, beanName, scope, dlbf, builder);
            if (bd != null)
                break;
        }
    }

    if (bd == null)
        bd = builder.getBeanDefinition();

    if (beanInfo != null)
        bd.setAttribute(relativePath, beanInfo.relativePath);
    bd.setScope(scope);

    dlbf.registerBeanDefinition(beanName, bd);

    boolean isUpdate = beanInfo != null ? beanInfo.isUpdate : false;
    String relativePath = beanInfo != null ? beanInfo.relativePath : "";

    BeanDefinitionWrap beanDefinitionWrap = new BeanDefinitionWrap(beanName, bd);

    if (ContextRefresher.class.isAssignableFrom(clz)) {
        ContextHolder.refreshers.register(beanName);
    }

    logRegist(isUpdate ? Info.Updated : Info.Registed, relativePath, beanName, scope);
    return beanDefinitionWrap;
}

From source file:org.jacp.project.processor.StatelessScopedPostProcessor.java

public void postProcessBeanFactory(final ConfigurableListableBeanFactory factory) throws BeansException {
    final String[] stateless = factory.getBeanNamesForType(AStatelessCallbackComponent.class);
    for (final String beanName : stateless) {
        final BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
        beanDefinition.setScope("prototype");
    }//  w  w  w. j  a  v a2 s  .  com
}

From source file:com.helpinput.spring.registinerceptor.ProxybeanRegistInterceptor.java

@Override
public BeanDefinition beforeRegist(Class<?> clz, String beanName, String scope, DefaultListableBeanFactory dlbf,
        BeanDefinitionBuilder builder) {
    BeanDefinition bd = null;//from   ww  w  .  jav  a  2  s. c  o  m

    if (getCondition(clz)) {
        Parent ann = clz.getAnnotation(Parent.class);
        String parentName = ann.value();
        String property = ann.property();
        if (Utils.hasLength(parentName) && Utils.hasLength(property)) {

            BeanDefinition parent = (GenericBeanDefinition) dlbf.getBeanDefinition(parentName);
            if (parent != null) {
                String baseBeanName = beanName + "$$$$";
                BeanRegister.removeBean(dlbf, null, baseBeanName);
                BeanDefinition basebd = builder.getBeanDefinition();
                basebd.setScope(scope);
                dlbf.registerBeanDefinition(baseBeanName, basebd);

                bd = new GenericBeanDefinition();

                bd.setParentName(parentName);
                List<PropertyValue> propertyValueList = bd.getPropertyValues().getPropertyValueList();
                RuntimeBeanReference reference = new RuntimeBeanReference(baseBeanName);
                PropertyValue pValue = new PropertyValue(property, reference);
                propertyValueList.add(pValue);

                //dlbf.getBean(baseBeanName);
                return bd;
            }
        }
    }
    return null;
}

From source file:org.jacpfx.spring.processor.StatelessScopedPostProcessor.java

@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory factory) throws BeansException {
    final String[] stateless = factory.getBeanNamesForType(CallbackComponent.class);
    for (final String beanName : stateless) {
        final BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
        final String className = beanDefinition.getBeanClassName();
        try {//from   w w  w.  j a va2 s .  co m
            Class<?> clazz = Class.forName(className);
            if (clazz.isAnnotationPresent(Stateless.class))
                beanDefinition.setScope("prototype");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}

From source file:com.github.persapiens.jsfboot.annotations.JsfCdiToSpringBeanNameGenerator.java

@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    String result = super.generateBeanName(definition, registry);

    if (definition instanceof AnnotatedBeanDefinition) {
        AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;

        String scopeName = JsfCdiToSpring.scopeName(annDef.getMetadata().getAnnotationTypes());
        if (scopeName != null) {
            definition.setScope(scopeName);

            logger.debug(//  ww w  .j av a 2 s  .c  om
                    definition.getBeanClassName() + " - Scope(" + definition.getScope().toUpperCase() + ")");
        }
    }

    return result;
}

From source file:org.joinfaces.annotations.JsfCdiToSpringBeanFactoryPostProcessor.java

/**
 * Checks how is bean defined and deduces scope name from JSF CDI annotations.
 *
 * @param definition beanDefinition/*from w w w. j  a va2 s  .  co m*/
 */
private void registerJsfCdiToSpring(BeanDefinition definition) {

    if (definition instanceof AnnotatedBeanDefinition) {
        AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;

        String scopeName = null;
        // firstly check whether bean is defined via configuration
        if (annDef.getFactoryMethodMetadata() != null) {
            scopeName = JsfCdiToSpring.deduceScopeName(annDef.getFactoryMethodMetadata());
        } else {
            // fallback to type
            scopeName = JsfCdiToSpring.deduceScopeName(annDef.getMetadata().getAnnotationTypes());
        }

        if (scopeName != null) {
            definition.setScope(scopeName);

            logger.debug(
                    definition.getBeanClassName() + " - Scope(" + definition.getScope().toUpperCase() + ")");
        }
    }
}

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

/**
 * Prepare the script beans in the internal BeanFactory that this
 * post-processor uses. Each original bean definition will be split
 * into a ScriptFactory definition and a scripted object definition.
 * @param bd the original bean definition in the main BeanFactory
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptedObjectBeanName the name of the internal scripted object bean
 *///w ww .j a va2 s  .c o  m
protected void prepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName,
        String scriptedObjectBeanName) {

    // Avoid recreation of the script bean definition in case of a prototype.
    synchronized (this.scriptBeanFactory) {
        if (!this.scriptBeanFactory.containsBeanDefinition(scriptedObjectBeanName)) {

            this.scriptBeanFactory.registerBeanDefinition(scriptFactoryBeanName,
                    createScriptFactoryBeanDefinition(bd));
            ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName,
                    ScriptFactory.class);
            ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName,
                    scriptFactory.getScriptSourceLocator());
            Class<?>[] interfaces = scriptFactory.getScriptInterfaces();

            Class<?>[] scriptedInterfaces = interfaces;
            if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
                Class<?> configInterface = createConfigInterface(bd, interfaces);
                scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
            }

            BeanDefinition objectBd = createScriptedObjectBeanDefinition(bd, scriptFactoryBeanName,
                    scriptSource, scriptedInterfaces);
            long refreshCheckDelay = resolveRefreshCheckDelay(bd);
            if (refreshCheckDelay >= 0) {
                objectBd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
            }

            this.scriptBeanFactory.registerBeanDefinition(scriptedObjectBeanName, objectBd);
        }
    }
}

From source file:org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor.java

protected void validateBeanScope(String beanName, BeanDefinition bd) {

    if (bd != null && StringUtils.isEmpty(bd.getScope())) {
        logger.info("Setting '" + beanName + "' with 'singleton' scope");
        bd.setScope(BeanDefinition.SCOPE_SINGLETON);
    }//w ww.  j  av a2  s  .  c  om
}

From source file:org.springframework.scripting.support.ScriptFactoryPostProcessor.java

/**
 * Prepare the script beans in the internal BeanFactory that this
 * post-processor uses. Each original bean definition will be split
 * into a ScriptFactory definition and a scripted object definition.
 * @param bd the original bean definition in the main BeanFactory
 * @param scriptFactoryBeanName the name of the internal ScriptFactory bean
 * @param scriptedObjectBeanName the name of the internal scripted object bean
 *//* w ww  .  j  av a  2 s  .  c o m*/
protected void prepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName,
        String scriptedObjectBeanName) {
    // Avoid recreation of the script bean definition in case of a prototype.
    synchronized (this.scriptBeanFactory) {
        if (!this.scriptBeanFactory.containsBeanDefinition(scriptedObjectBeanName)) {

            this.scriptBeanFactory.registerBeanDefinition(scriptFactoryBeanName,
                    createScriptFactoryBeanDefinition(bd));
            ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName,
                    ScriptFactory.class);
            ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName,
                    scriptFactory.getScriptSourceLocator());
            Class<?>[] interfaces = scriptFactory.getScriptInterfaces();

            Class<?>[] scriptedInterfaces = interfaces;
            if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
                Class<?> configInterface = createConfigInterface(bd, interfaces);
                scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
            }

            BeanDefinition objectBd = createScriptedObjectBeanDefinition(bd, scriptFactoryBeanName,
                    scriptSource, scriptedInterfaces);
            long refreshCheckDelay = resolveRefreshCheckDelay(bd);
            if (refreshCheckDelay >= 0) {
                objectBd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
            }

            this.scriptBeanFactory.registerBeanDefinition(scriptedObjectBeanName, objectBd);
        }
    }
}

From source file:org.springframework.social.config.support.AbstractConnectionRepositoryConfigSupport.java

protected BeanDefinition registerConnectionRepository(BeanDefinitionRegistry registry,
        String usersConnectionRepositoryId, String connectionRepositoryId, String userIdSourceRef) {
    if (logger.isDebugEnabled()) {
        logger.debug("Registering ConnectionRepository bean");
    }//  ww w  .ja va2 s .  c om
    // TODO: Hackish use of SpEL to reference userIdSource
    BeanDefinition connectionRepositoryBD = BeanDefinitionBuilder.genericBeanDefinition()
            .addConstructorArgValue("#{" + userIdSourceRef + ".userId}").getBeanDefinition();
    connectionRepositoryBD.setFactoryBeanName(usersConnectionRepositoryId);
    connectionRepositoryBD.setFactoryMethodName(CREATE_CONNECTION_REPOSITORY_METHOD_NAME);
    connectionRepositoryBD.setScope("request");
    registry.registerBeanDefinition(connectionRepositoryId,
            decorateWithScopedProxy(connectionRepositoryId, connectionRepositoryBD, registry));
    return connectionRepositoryBD;
}