Example usage for org.springframework.beans.factory.config RuntimeBeanReference RuntimeBeanReference

List of usage examples for org.springframework.beans.factory.config RuntimeBeanReference RuntimeBeanReference

Introduction

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

Prototype

public RuntimeBeanReference(Class<?> beanType) 

Source Link

Document

Create a new RuntimeBeanReference to a bean of the given type.

Usage

From source file:org.apache.camel.spring.handler.CamelNamespaceHandler.java

private void registerEndpoint(Element childElement, ParserContext parserContext, String contextId) {
    String id = childElement.getAttribute("id");
    // must have an id to be registered
    if (ObjectHelper.isNotEmpty(id)) {
        BeanDefinition definition = endpointParser.parse(childElement, parserContext);
        definition.getPropertyValues().addPropertyValue("camelContext", new RuntimeBeanReference(contextId));
        // Need to add this dependency of CamelContext for Spring 3.0
        try {//  w  w  w  .  ja v  a 2  s .co  m
            Method method = definition.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(definition, (Object) new String[] { contextId });
        } catch (Exception e) {
            // Do nothing here
        }
        parserContext.registerBeanComponent(new BeanComponentDefinition(definition, id));
    }
}

From source file:org.beangle.spring.bind.AutoConfigProcessor.java

protected void autowire(String beanName, BeanDefinition mbd) {
    Map<String, PropertyDescriptor> properties = unsatisfiedNonSimpleProperties(mbd);
    for (Map.Entry<String, PropertyDescriptor> entry : properties.entrySet()) {
        String propertyName = entry.getKey();
        PropertyDescriptor pd = entry.getValue();
        if (Object.class.equals(pd.getPropertyType()))
            continue;
        MethodParameter methodParam = BeanUtils.getWriteMethodParameter(pd);
        List<String> beanNames = bindRegistry.getBeanNames(methodParam.getParameterType());
        boolean binded = false;
        if (beanNames.size() == 1) {
            mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(beanNames.get(0)));
            binded = true;//from   ww  w  .ja  v a 2  s  . co  m
        } else if (beanNames.size() > 1) {
            for (String name : beanNames) {
                if (name.equals(propertyName)) {
                    mbd.getPropertyValues().add(propertyName, new RuntimeBeanReference(propertyName));
                    binded = true;
                    break;
                }
            }
        }
        if (!binded) {
            if (beanNames.isEmpty()) {
                logger.debug(beanName + "'s " + propertyName + "  cannot  found candidate bean");
            } else {
                logger.warn(beanName + "'s " + propertyName + " expected single bean but found {} : {}",
                        beanNames.size(), beanNames);
            }
        }
    }
}

From source file:org.bytesoft.bytejta.supports.dubbo.TransactionConfigPostProcessor.java

public void initializeForProvider(ConfigurableListableBeanFactory beanFactory, String application,
        String refBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ServiceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("ref", new RuntimeBeanReference(refBeanName));
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "transaction");
    mpv.addPropertyValue("group", "org.bytesoft.bytejta");
    mpv.addPropertyValue("retries", "0");
    mpv.addPropertyValue("timeout", "5000");

    String skeletonBeanId = String.format("skeleton@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(skeletonBeanId, beanDef);
}

From source file:org.bytesoft.bytejta.supports.dubbo.TransactionConfigPostProcessor.java

public void initializeForConsumer(ConfigurableListableBeanFactory beanFactory, String application,
        String targetBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:reference id="yyy"
    // interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // timeout="6000" group="org.bytesoft.bytejta" loadbalance="transaction" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ReferenceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("timeout", "6000");
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "transaction");
    mpv.addPropertyValue("group", "org.bytesoft.bytejta");
    mpv.addPropertyValue("check", "false");

    String stubBeanId = String.format("stub@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(stubBeanId, beanDef);

    // <bean id="xxx" class="org.bytesoft.bytejta.supports.dubbo.TransactionBeanRegistry"
    // factory-method="getInstance">
    // <property name="consumeCoordinator" ref="yyy" />
    // </bean>
    BeanDefinition targetBeanDef = beanFactory.getBeanDefinition(targetBeanName);
    MutablePropertyValues targetMpv = targetBeanDef.getPropertyValues();
    targetMpv.addPropertyValue("consumeCoordinator", new RuntimeBeanReference(stubBeanId));
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableConfigPostProcessor.java

public void initializeForProvider(ConfigurableListableBeanFactory beanFactory, String application,
        String refBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:service interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // ref="dispatcherCoordinator" group="org.bytesoft.bytetcc" loadbalance="compensable" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ServiceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("ref", new RuntimeBeanReference(refBeanName));
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "compensable");
    mpv.addPropertyValue("group", "org.bytesoft.bytetcc");
    mpv.addPropertyValue("retries", "0");
    mpv.addPropertyValue("timeout", String.valueOf(1000L * 6));

    String skeletonBeanId = String.format("skeleton@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(skeletonBeanId, beanDef);
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableConfigPostProcessor.java

public void initializeForConsumer(ConfigurableListableBeanFactory beanFactory, String application,
        String targetBeanName) throws BeansException {
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    // <dubbo:reference id="yyy"
    // interface="org.bytesoft.bytejta.supports.wire.RemoteCoordinator"
    // timeout="3000" group="org.bytesoft.bytetcc" loadbalance="compensable" cluster="failfast" />
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ReferenceBean.class);

    MutablePropertyValues mpv = beanDef.getPropertyValues();
    mpv.addPropertyValue("interface", RemoteCoordinator.class.getName());
    mpv.addPropertyValue("timeout", "3000");
    mpv.addPropertyValue("cluster", "failfast");
    mpv.addPropertyValue("loadbalance", "compensable");
    mpv.addPropertyValue("group", "org.bytesoft.bytetcc");

    String stubBeanId = String.format("stub@%s", RemoteCoordinator.class.getName());
    registry.registerBeanDefinition(stubBeanId, beanDef);

    // <bean id="xxx" class="org.bytesoft.bytetcc.supports.dubbo.CompensableBeanRegistry"
    // factory-method="getInstance">
    // <property name="consumeCoordinator" ref="yyy" />
    // </bean>
    BeanDefinition targetBeanDef = beanFactory.getBeanDefinition(targetBeanName);
    MutablePropertyValues targetMpv = targetBeanDef.getPropertyValues();
    targetMpv.addPropertyValue("consumeCoordinator", new RuntimeBeanReference(stubBeanId));
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableCoordinatorProcessor.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

    String application = null;//from w  w  w. ja v a 2s  .  co  m
    String[] appBeanNameArray = beanFactory.getBeanNamesForType(ApplicationConfig.class);
    if (appBeanNameArray != null && appBeanNameArray.length == 1) {
        String beanName = appBeanNameArray[0];
        BeanDefinition beanDef = registry.getBeanDefinition(beanName);
        MutablePropertyValues values = beanDef.getPropertyValues();
        String propertyName = "name";
        PropertyValue pv = values.getPropertyValue(propertyName);
        application = pv == null ? null : (String) pv.getValue();
    }

    if (StringUtils.isBlank(application)) {
        throw new FatalBeanException(
                "There is no application name specified, or there is more than one application name!");
    }

    String[] coordinatorNameArray = beanFactory.getBeanNamesForType(TransactionCoordinator.class);
    if (coordinatorNameArray != null && coordinatorNameArray.length == 1) {
        String beanName = coordinatorNameArray[0];

        GenericBeanDefinition beanDef = new GenericBeanDefinition();
        beanDef.setBeanClass(com.alibaba.dubbo.config.spring.ServiceBean.class);
        MutablePropertyValues values = beanDef.getPropertyValues();
        values.addPropertyValue("group", application);
        values.addPropertyValue("interface", TransactionCoordinator.class.getName());
        values.addPropertyValue("ref", new RuntimeBeanReference(beanName));
        values.addPropertyValue("retries", "0");
        values.addPropertyValue("timeout", String.valueOf(1000L * 6));
        registry.registerBeanDefinition(String.format("%s@%s", beanName, application), beanDef);
    } else {
        throw new FatalBeanException("No available(or redundant) org.bytesoft.bytetcc.TransactionCoordinator!");
    }

}

From source file:org.directwebremoting.spring.DwrNamespaceHandler.java

/**
 * Registers a new {@link org.directwebremoting.extend.Creator} in the registry using name <code>javascript</code>.
 * @param registry The definition of all the Beans
 * @param javascript The name of the bean in the registry.
 * @param creatorConfig/*from   www .j  a  v a 2  s  . c o  m*/
 * @param params
 * @param children The node list to check for nested elements
 */
@SuppressWarnings("unchecked")
protected void registerCreator(BeanDefinitionRegistry registry, String javascript,
        BeanDefinitionBuilder creatorConfig, Map<String, String> params, NodeList children) {
    registerSpringConfiguratorIfNecessary(registry);

    List<String> includes = new ArrayList<String>();
    creatorConfig.addPropertyValue("includes", includes);

    List<String> excludes = new ArrayList<String>();
    creatorConfig.addPropertyValue("excludes", excludes);

    Properties auth = new Properties();
    creatorConfig.addPropertyValue("auth", auth);

    // check to see if there are any nested elements here
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);

        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.COMMENT_NODE) {
            continue;
        }

        Element child = (Element) node;

        if ("dwr:latencyfilter".equals(node.getNodeName())) {
            BeanDefinitionBuilder beanFilter = BeanDefinitionBuilder
                    .rootBeanDefinition(ExtraLatencyAjaxFilter.class);
            beanFilter.addPropertyValue("delay", child.getAttribute("delay"));
            BeanDefinitionHolder holder2 = new BeanDefinitionHolder(beanFilter.getBeanDefinition(),
                    "__latencyFilter_" + javascript);
            BeanDefinitionReaderUtils.registerBeanDefinition(holder2, registry);

            ManagedList filterList = new ManagedList();
            filterList.add(new RuntimeBeanReference("__latencyFilter_" + javascript));
            creatorConfig.addPropertyValue("filters", filterList);
        } else if ("dwr:include".equals(node.getNodeName())) {
            includes.add(child.getAttribute("method"));
        } else if ("dwr:exclude".equals(node.getNodeName())) {
            excludes.add(child.getAttribute("method"));
        } else if ("dwr:auth".equals(node.getNodeName())) {
            auth.setProperty(child.getAttribute("method"), child.getAttribute("role"));
        } else if ("dwr:convert".equals(node.getNodeName())) {
            Element element = (Element) node;
            String type = element.getAttribute("type");
            String className = element.getAttribute("class");

            ConverterConfig converterConfig = new ConverterConfig();
            converterConfig.setType(type);
            parseConverterSettings(converterConfig, element);
            lookupConverters(registry).put(className, converterConfig);
        } else if ("dwr:filter".equals(node.getNodeName())) {
            Element element = (Element) node;
            String filterClass = element.getAttribute("class");
            List<Element> filterParamElements = DomUtils.getChildElementsByTagName(element, "param");
            BeanDefinitionBuilder beanFilter;
            try {
                beanFilter = BeanDefinitionBuilder.rootBeanDefinition(ClassUtils.forName(filterClass));
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("DWR filter class '" + filterClass + "' was not found. "
                        + "Check the class name specified in <dwr:filter class=\"" + filterClass
                        + "\" /> exists");
            }
            for (Element filterParamElement : filterParamElements) {
                beanFilter.addPropertyValue(filterParamElement.getAttribute("name"),
                        filterParamElement.getAttribute("value"));
            }
            BeanDefinitionHolder holder2 = new BeanDefinitionHolder(beanFilter.getBeanDefinition(),
                    "__filter_" + filterClass + "_" + javascript);
            BeanDefinitionReaderUtils.registerBeanDefinition(holder2, registry);

            ManagedList filterList = new ManagedList();
            filterList.add(new RuntimeBeanReference("__filter_" + filterClass + "_" + javascript));
            creatorConfig.addPropertyValue("filters", filterList);
        } else if ("dwr:param".equals(node.getNodeName())) {
            Element element = (Element) node;
            String name = element.getAttribute("name");
            String value = element.getAttribute("value");
            params.put(name, value);
        } else {
            throw new RuntimeException("an unknown dwr:remote sub node was fouund: " + node.getNodeName());
        }
    }
    creatorConfig.addPropertyValue("params", params);

    String creatorConfigName = "__" + javascript;
    BeanDefinitionHolder holder3 = new BeanDefinitionHolder(creatorConfig.getBeanDefinition(),
            creatorConfigName);
    BeanDefinitionReaderUtils.registerBeanDefinition(holder3, registry);

    lookupCreators(registry).put(javascript, new RuntimeBeanReference(creatorConfigName));
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

private Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
    String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'"
            : "<constructor-arg> element";

    // Should only have one child element: ref, value, list, etc.
    NodeList nl = ele.getChildNodes();
    Element subElement = null;//from w  ww.j  a  v  a  2  s. c o m
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element
                && !DomUtils.nodeNameEquals(node, BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT)) {
            // Child element is what we're looking for.
            if (subElement != null) {
                error(elementName + " must not contain more than one sub-element", ele);
            } else {
                subElement = (Element) node;
            }
        }
    }

    boolean hasRefAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
    boolean hasValueAttribute = ele.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
    if ((hasRefAttribute && hasValueAttribute)
            || ((hasRefAttribute || hasValueAttribute) && subElement != null)) {
        error(elementName
                + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element",
                ele);
    }

    if (hasRefAttribute) {
        String refName = ele.getAttribute(BeanDefinitionParserDelegate.REF_ATTRIBUTE);
        if (!StringUtils.hasText(refName)) {
            error(elementName + " contains empty 'ref' attribute", ele);
        }
        RuntimeBeanReference ref = new RuntimeBeanReference(refName);
        ref.setSource(parserContext.extractSource(ele));
        return ref;
    } else if (hasValueAttribute) {
        TypedStringValue valueHolder = new TypedStringValue(
                ele.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE));
        valueHolder.setSource(parserContext.extractSource(ele));
        return valueHolder;
    } else if (subElement != null) {
        return parsePropertySubElement(subElement, bd, null);
    } else {
        // Neither child element nor "ref" or "value" attribute found.
        error(elementName + " must specify a ref or value", ele);
        return null;
    }
}

From source file:org.eclipse.gemini.blueprint.blueprint.config.internal.BlueprintParser.java

/**
 * Parse a map element./*from   w  ww . j  av a  2s. com*/
 */
public Map<?, ?> parseMapElement(Element mapEle, BeanDefinition bd) {
    String defaultKeyType = mapEle.getAttribute(BeanDefinitionParserDelegate.KEY_TYPE_ATTRIBUTE);
    String defaultValueType = mapEle.getAttribute(BeanDefinitionParserDelegate.VALUE_TYPE_ATTRIBUTE);

    List<Element> entryEles = DomUtils.getChildElementsByTagName(mapEle,
            BeanDefinitionParserDelegate.ENTRY_ELEMENT);
    ManagedMap<Object, Object> map = new ManagedMap<Object, Object>(entryEles.size());
    map.setSource(extractSource(mapEle));
    map.setKeyTypeName(defaultKeyType);
    map.setValueTypeName(defaultValueType);
    map.setMergeEnabled(parseMergeAttribute(mapEle));

    for (Element entryEle : entryEles) {
        // Should only have one value child element: ref, value, list, etc.
        // Optionally, there might be a key child element.
        NodeList entrySubNodes = entryEle.getChildNodes();
        Element keyEle = null;
        Element valueEle = null;
        for (int j = 0; j < entrySubNodes.getLength(); j++) {
            Node node = entrySubNodes.item(j);
            if (node instanceof Element) {
                Element candidateEle = (Element) node;
                if (DomUtils.nodeNameEquals(candidateEle, BeanDefinitionParserDelegate.KEY_ELEMENT)) {
                    if (keyEle != null) {
                        error("<entry> element is only allowed to contain one <key> sub-element", entryEle);
                    } else {
                        keyEle = candidateEle;
                    }
                } else {
                    // Child element is what we're looking for.
                    if (valueEle != null) {
                        error("<entry> element must not contain more than one value sub-element", entryEle);
                    } else {
                        valueEle = candidateEle;
                    }
                }
            }
        }

        // Extract key from attribute or sub-element.
        Object key = null;
        boolean hasKeyAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE);
        boolean hasKeyRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
        if ((hasKeyAttribute && hasKeyRefAttribute)
                || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element", entryEle);
        }
        if (hasKeyAttribute) {
            key = buildTypedStringValueForMap(entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_ATTRIBUTE),
                    defaultKeyType, entryEle);
        } else if (hasKeyRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.KEY_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'key-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            key = ref;
        } else if (keyEle != null) {
            key = parseKeyElement(keyEle, bd, defaultKeyType);
        } else {
            error("<entry> element must specify a key", entryEle);
        }

        // Extract value from attribute or sub-element.
        Object value = null;
        boolean hasValueAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE);
        boolean hasValueRefAttribute = entryEle.hasAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
        if ((hasValueAttribute && hasValueRefAttribute)
                || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) {
            error("<entry> element is only allowed to contain either "
                    + "'value' attribute OR 'value-ref' attribute OR <value> sub-element", entryEle);
        }
        if (hasValueAttribute) {
            value = buildTypedStringValueForMap(
                    entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_ATTRIBUTE), defaultValueType,
                    entryEle);
        } else if (hasValueRefAttribute) {
            String refName = entryEle.getAttribute(BeanDefinitionParserDelegate.VALUE_REF_ATTRIBUTE);
            if (!StringUtils.hasText(refName)) {
                error("<entry> element contains empty 'value-ref' attribute", entryEle);
            }
            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(extractSource(entryEle));
            value = ref;
        } else if (valueEle != null) {
            value = parsePropertySubElement(valueEle, bd, defaultValueType);
        } else {
            error("<entry> element must specify a value", entryEle);
        }

        // Add final key and value to the Map.
        map.put(key, value);
    }

    return map;
}