Search in sources :

Example 71 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project geronimo-xbean by apache.

the class XBeanNamespaceHandler method parseBeanFromExtensionElement.

private BeanDefinitionHolder parseBeanFromExtensionElement(Element element, MappingMetaData metadata, String className) {
    Element original = cloneElement(element);
    // lets assume the class name == the package name plus the
    element.setAttributeNS(null, "class", className);
    addSpringAttributeValues(className, element);
    BeanDefinitionHolder definition = parserContext.getDelegate().parseBeanDefinitionElement(element, null);
    addAttributeProperties(definition, metadata, className, original);
    addContentProperty(definition, metadata, element);
    addNestedPropertyElements(definition, metadata, className, element);
    qnameHelper.coerceNamespaceAwarePropertyValues(definition.getBeanDefinition(), element);
    declareLifecycleMethods(definition, metadata, element);
    resolveBeanClass((AbstractBeanDefinition) definition.getBeanDefinition(), definition.getBeanName());
    namedConstructorArgs.processParameters(definition, metadata);
    return definition;
}
Also used : Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder)

Example 72 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project ma-core-public by infiniteautomation.

the class DwrNamespaceHandler method registerCreator.

/**
 * 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 beanCreator The {@link org.directwebremoting.extend.Creator} to register.
 * @param children The node list to check for nested elements
 */
protected void registerCreator(BeanDefinitionRegistry registry, String javascript, BeanDefinitionBuilder creatorConfig, Map params, NodeList children) {
    registerSpringConfiguratorIfNecessary(registry);
    List includes = new ArrayList();
    creatorConfig.addPropertyValue("includes", includes);
    List excludes = new ArrayList();
    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 (node.getNodeName().equals("dwr:latencyfilter")) {
            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 (node.getNodeName().equals("dwr:include")) {
            includes.add(child.getAttribute("method"));
        } else if (node.getNodeName().equals("dwr:exclude")) {
            excludes.add(child.getAttribute("method"));
        } else if (node.getNodeName().equals("dwr:auth")) {
            auth.setProperty(child.getAttribute("method"), child.getAttribute("role"));
        } else if (node.getNodeName().equals("dwr:convert")) {
            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 (node.getNodeName().equals("dwr:filter")) {
            Element element = (Element) node;
            String filterClass = element.getAttribute("class");
            BeanDefinitionBuilder beanFilter;
            try {
                beanFilter = BeanDefinitionBuilder.rootBeanDefinition(ClassUtils.forName(filterClass, this.getClass().getClassLoader()));
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("DWR filter class '" + filterClass + "' was not found. " + "Check the class name specified in <dwr:filter class=\"" + filterClass + "\" /> exists");
            }
            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 (node.getNodeName().equals("dwr:param")) {
            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));
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ManagedList(org.springframework.beans.factory.support.ManagedList) Properties(java.util.Properties) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ArrayList(java.util.ArrayList) ManagedList(org.springframework.beans.factory.support.ManagedList) NodeList(org.w3c.dom.NodeList) List(java.util.List) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 73 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project motan by weibocom.

the class MotanBeanDefinitionParser method parse.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) throws ClassNotFoundException {
    RootBeanDefinition bd = new RootBeanDefinition();
    bd.setBeanClass(beanClass);
    // 不允许lazy init
    bd.setLazyInit(false);
    // 如果没有id则按照规则生成一个id,注册id到context中
    String id = element.getAttribute("id");
    if ((id == null || id.length() == 0) && required) {
        String generatedBeanName = element.getAttribute("name");
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = element.getAttribute("class");
        }
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = beanClass.getName();
        }
        id = generatedBeanName;
        int counter = 2;
        while (parserContext.getRegistry().containsBeanDefinition(id)) {
            id = generatedBeanName + (counter++);
        }
    }
    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new IllegalStateException("Duplicate spring bean id " + id);
        }
        parserContext.getRegistry().registerBeanDefinition(id, bd);
    }
    bd.getPropertyValues().addPropertyValue("id", id);
    if (ProtocolConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.protocolDefineNames.add(id);
    } else if (RegistryConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.registryDefineNames.add(id);
    } else if (BasicServiceInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicServiceConfigDefineNames.add(id);
    } else if (BasicRefererInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicRefererConfigDefineNames.add(id);
    } else if (ServiceConfigBean.class.equals(beanClass)) {
        String className = element.getAttribute("class");
        if (className != null && className.length() > 0) {
            RootBeanDefinition classDefinition = new RootBeanDefinition();
            classDefinition.setBeanClass(Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
            classDefinition.setLazyInit(false);
            parseProperties(element.getChildNodes(), classDefinition);
            bd.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
        }
    }
    Set<String> props = new HashSet<String>();
    ManagedMap parameters = null;
    // 把配置文件中的可以set的属性放到bd中
    for (Method setter : beanClass.getMethods()) {
        String name = setter.getName();
        // 必须是setXXX
        if (name.length() <= 3 || !name.startsWith("set") || !Modifier.isPublic(setter.getModifiers()) || setter.getParameterTypes().length != 1) {
            continue;
        }
        String property = (name.substring(3, 4).toLowerCase() + name.substring(4)).replaceAll("_", "-");
        props.add(property);
        if ("id".equals(property)) {
            bd.getPropertyValues().addPropertyValue("id", id);
            continue;
        }
        String value = element.getAttribute(property);
        if ("methods".equals(property)) {
            parseMethods(id, element.getChildNodes(), bd, parserContext);
        }
        if (StringUtils.isBlank(value)) {
            continue;
        }
        value = value.trim();
        if (value.length() == 0) {
            continue;
        }
        Object reference;
        if ("ref".equals(property)) {
            if (parserContext.getRegistry().containsBeanDefinition(value)) {
                BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                if (!refBean.isSingleton()) {
                    throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value + "\" scope=\"singleton\" ...>");
                }
            }
            reference = new RuntimeBeanReference(value);
        } else if ("protocol".equals(property) && !StringUtils.isBlank(value)) {
            if (!value.contains(",")) {
                reference = new RuntimeBeanReference(value);
            } else {
                parseMultiRef("protocols", value, bd, parserContext);
                reference = null;
            }
        } else if ("registry".equals(property)) {
            parseMultiRef("registries", value, bd, parserContext);
            reference = null;
        } else if ("basicService".equals(property) || "basicReferer".equals(property) || "extConfig".equals(property) || "proxyRegistry".equals(property)) {
            reference = new RuntimeBeanReference(value);
        } else {
            reference = new TypedStringValue(value);
        }
        if (reference != null) {
            bd.getPropertyValues().addPropertyValue(property, reference);
        }
    }
    if (ProtocolConfig.class.equals(beanClass)) {
        // 把剩余的属性放到protocol的parameters里面
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        bd.getPropertyValues().addPropertyValue("parameters", parameters);
    }
    return bd;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) ManagedMap(org.springframework.beans.factory.support.ManagedMap) HashSet(java.util.HashSet)

Example 74 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project motan by weibocom.

the class MotanBeanDefinitionParser method parseMethods.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition, ParserContext parserContext) throws ClassNotFoundException {
    if (nodeList != null && nodeList.getLength() > 0) {
        ManagedList methods = null;
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {
                    String methodName = element.getAttribute("name");
                    if (methodName == null || methodName.length() == 0) {
                        throw new IllegalStateException("<motan:method> name attribute == null");
                    }
                    if (methods == null) {
                        methods = new ManagedList();
                    }
                    BeanDefinition methodBeanDefinition = parse((Element) node, parserContext, MethodConfig.class, false);
                    String name = id + "." + methodName;
                    BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(methodBeanDefinition, name);
                    methods.add(methodBeanDefinitionHolder);
                }
            }
        }
        if (methods != null) {
            beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
        }
    }
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 75 with BeanDefinitionHolder

use of org.springframework.beans.factory.config.BeanDefinitionHolder in project redisson by redisson.

the class RedissonNamespaceParserSupport method registerBeanDefinition.

public BeanComponentDefinition registerBeanDefinition(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
    BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), getId(element, builder, parserContext), parseAliase(element));
    BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
    BeanComponentDefinition componentDefinition = new BeanComponentDefinition(holder);
    parserContext.registerComponent(componentDefinition);
    return componentDefinition;
}
Also used : BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition)

Aggregations

BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)94 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)30 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)18 Element (org.w3c.dom.Element)18 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)16 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)16 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)12 ManagedList (org.springframework.beans.factory.support.ManagedList)11 Node (org.w3c.dom.Node)11 ArrayList (java.util.ArrayList)8 ManagedMap (org.springframework.beans.factory.support.ManagedMap)6 LinkedHashSet (java.util.LinkedHashSet)5 Test (org.junit.jupiter.api.Test)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)5 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 NodeList (org.w3c.dom.NodeList)5 Method (java.lang.reflect.Method)4 HashSet (java.util.HashSet)4