Search in sources :

Example 21 with PropertyValue

use of org.springframework.beans.PropertyValue in project camel by apache.

the class AbstractCxfBeanDefinitionParser method getPropertyMap.

@SuppressWarnings("unchecked")
protected Map<String, Object> getPropertyMap(BeanDefinitionBuilder bean, boolean lazyInstantiation) {
    PropertyValue propertyValue = bean.getBeanDefinition().getPropertyValues().getPropertyValue("properties");
    Map<String, Object> map = null;
    if (propertyValue == null) {
        if (lazyInstantiation) {
            map = new HashMap<String, Object>();
            bean.addPropertyValue("properties", map);
        }
    } else {
        map = (Map<String, Object>) propertyValue.getValue();
    }
    return map;
}
Also used : PropertyValue(org.springframework.beans.PropertyValue)

Example 22 with PropertyValue

use of org.springframework.beans.PropertyValue in project redisson by redisson.

the class RemoteInvocationOptionDecorator method decorate.

@Override
public void decorate(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, RedissonNamespaceParserSupport helper) {
    if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_INVOCATION_OPTIONS_ELEMENT)) {
        Element options = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_INVOCATION_OPTIONS_ELEMENT);
        String optionBeanId = invokeOptions(options, parserContext, helper);
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_NO_ACK_ELEMENT)) {
            helper.invoker(optionBeanId, "noAck", null, parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT)) {
            Element remoteAck = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT);
            Assert.state(helper.hasAttribute(remoteAck, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE), "Missing \"" + RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE + "\" attribute in \"" + RedissonNamespaceParserSupport.REMOTE_ACK_ELEMENT + "\" element.");
            ArrayList args = new ArrayList(2);
            args.add(helper.getAttribute(remoteAck, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE));
            if (helper.hasAttribute(remoteAck, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE)) {
                args.add(helper.getAttribute(remoteAck, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE));
            }
            helper.invoker(optionBeanId, "expectAckWithin", args.toArray(), parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_NO_RESULT_ELEMENT)) {
            helper.invoker(optionBeanId, "noResult", null, parserContext);
        }
        if (helper.hasElement(element, RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT)) {
            Element remoteResult = helper.getSingleElement(element, RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT);
            Assert.state(helper.hasAttribute(remoteResult, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE), "Missing \"" + RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE + "\" attribute in \"" + RedissonNamespaceParserSupport.REMOTE_RESULT_ELEMENT + "\" element.");
            ArrayList args = new ArrayList(2);
            args.add(helper.getAttribute(remoteResult, RedissonNamespaceParserSupport.WITHIN_ATTRIBUTE));
            if (helper.hasAttribute(remoteResult, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE)) {
                args.add(helper.getAttribute(remoteResult, RedissonNamespaceParserSupport.TIME_UNIT_ATTRIBUTE));
            }
            helper.invoker(optionBeanId, "expectResultWithin", args.toArray(), parserContext);
        }
        MutablePropertyValues properties = builder.getRawBeanDefinition().getPropertyValues();
        PropertyValue propertyValue = properties.getPropertyValue("arguments");
        ManagedList<Object> args = new ManagedList();
        args.addAll(Arrays.asList((Object[]) propertyValue.getValue()));
        args.add(new RuntimeBeanReference(optionBeanId));
        properties.removePropertyValue("arguments");
        properties.addPropertyValue("arguments", args);
    }
}
Also used : Element(org.w3c.dom.Element) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) ManagedList(org.springframework.beans.factory.support.ManagedList) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Example 23 with PropertyValue

use of org.springframework.beans.PropertyValue in project dubbo by alibaba.

the class DubboBeanDefinitionParser method parse.

@SuppressWarnings("unchecked")
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(beanClass);
    beanDefinition.setLazyInit(false);
    String id = element.getAttribute("id");
    if ((id == null || id.length() == 0) && required) {
        String generatedBeanName = element.getAttribute("name");
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            if (ProtocolConfig.class.equals(beanClass)) {
                generatedBeanName = "dubbo";
            } else {
                generatedBeanName = element.getAttribute("interface");
            }
        }
        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, beanDefinition);
        beanDefinition.getPropertyValues().addPropertyValue("id", id);
    }
    if (ProtocolConfig.class.equals(beanClass)) {
        for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
            BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
            PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
            if (property != null) {
                Object value = property.getValue();
                if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
                    definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
                }
            }
        }
    } else if (ServiceBean.class.equals(beanClass)) {
        String className = element.getAttribute("class");
        if (className != null && className.length() > 0) {
            RootBeanDefinition classDefinition = new RootBeanDefinition();
            classDefinition.setBeanClass(ReflectUtils.forName(className));
            classDefinition.setLazyInit(false);
            parseProperties(element.getChildNodes(), classDefinition);
            beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
        }
    } else if (ProviderConfig.class.equals(beanClass)) {
        parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
    } else if (ConsumerConfig.class.equals(beanClass)) {
        parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
    }
    Set<String> props = new HashSet<String>();
    ManagedMap parameters = null;
    for (Method setter : beanClass.getMethods()) {
        String name = setter.getName();
        if (name.length() > 3 && name.startsWith("set") && Modifier.isPublic(setter.getModifiers()) && setter.getParameterTypes().length == 1) {
            Class<?> type = setter.getParameterTypes()[0];
            String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
            props.add(property);
            Method getter = null;
            try {
                getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
            } catch (NoSuchMethodException e) {
                try {
                    getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e2) {
                }
            }
            if (getter == null || !Modifier.isPublic(getter.getModifiers()) || !type.equals(getter.getReturnType())) {
                continue;
            }
            if ("parameters".equals(property)) {
                parameters = parseParameters(element.getChildNodes(), beanDefinition);
            } else if ("methods".equals(property)) {
                parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
            } else if ("arguments".equals(property)) {
                parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
            } else {
                String value = element.getAttribute(property);
                if (value != null) {
                    value = value.trim();
                    if (value.length() > 0) {
                        if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                            RegistryConfig registryConfig = new RegistryConfig();
                            registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                            beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
                        } else if ("registry".equals(property) && value.indexOf(',') != -1) {
                            parseMultiRef("registries", value, beanDefinition, parserContext);
                        } else if ("provider".equals(property) && value.indexOf(',') != -1) {
                            parseMultiRef("providers", value, beanDefinition, parserContext);
                        } else if ("protocol".equals(property) && value.indexOf(',') != -1) {
                            parseMultiRef("protocols", value, beanDefinition, parserContext);
                        } else {
                            Object reference;
                            if (isPrimitive(type)) {
                                if ("async".equals(property) && "false".equals(value) || "timeout".equals(property) && "0".equals(value) || "delay".equals(property) && "0".equals(value) || "version".equals(property) && "0.0.0".equals(value) || "stat".equals(property) && "-1".equals(value) || "reliable".equals(property) && "false".equals(value)) {
                                    // 兼容旧版本xsd中的default值
                                    value = null;
                                }
                                reference = value;
                            } else if ("protocol".equals(property) && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value) && (!parserContext.getRegistry().containsBeanDefinition(value) || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                if ("dubbo:provider".equals(element.getTagName())) {
                                    logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
                                }
                                // 兼容旧版本配置
                                ProtocolConfig protocol = new ProtocolConfig();
                                protocol.setName(value);
                                reference = protocol;
                            } else if ("monitor".equals(property) && (!parserContext.getRegistry().containsBeanDefinition(value) || !MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                // 兼容旧版本配置
                                reference = convertMonitor(value);
                            } else if ("onreturn".equals(property)) {
                                int index = value.lastIndexOf(".");
                                String returnRef = value.substring(0, index);
                                String returnMethod = value.substring(index + 1);
                                reference = new RuntimeBeanReference(returnRef);
                                beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                            } else if ("onthrow".equals(property)) {
                                int index = value.lastIndexOf(".");
                                String throwRef = value.substring(0, index);
                                String throwMethod = value.substring(index + 1);
                                reference = new RuntimeBeanReference(throwRef);
                                beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                            } else {
                                if ("ref".equals(property) && 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);
                            }
                            beanDefinition.getPropertyValues().addPropertyValue(property, reference);
                        }
                    }
                }
            }
        }
    }
    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));
        }
    }
    if (parameters != null) {
        beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
    }
    return beanDefinition;
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) Node(org.w3c.dom.Node) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ServiceBean(com.alibaba.dubbo.config.spring.ServiceBean) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConsumerConfig(com.alibaba.dubbo.config.ConsumerConfig) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Protocol(com.alibaba.dubbo.rpc.Protocol) ManagedMap(org.springframework.beans.factory.support.ManagedMap) HashSet(java.util.HashSet) NamedNodeMap(org.w3c.dom.NamedNodeMap) PropertyValue(org.springframework.beans.PropertyValue) Method(java.lang.reflect.Method) ReferenceBean(com.alibaba.dubbo.config.spring.ReferenceBean) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue)

Example 24 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-framework by spring-projects.

the class DispatcherServletTests method detectHandlerMappingFromParent.

@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
    // create a parent context that includes a mapping
    StaticWebApplicationContext parent = new StaticWebApplicationContext();
    parent.setServletContext(getServletContext());
    parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
    parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
    parent.refresh();
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // will have parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    ServletConfig config = new MockServletConfig(getServletContext(), "complex");
    config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
    complexDispatcherServlet.init(config);
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertFalse("Matched through parent controller/handler pair: not response=" + response.getStatus(), response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) PropertyValue(org.springframework.beans.PropertyValue) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with PropertyValue

use of org.springframework.beans.PropertyValue in project spring-boot by spring-projects.

the class PropertySourcesPropertyValues method getPropertyValue.

@Override
public PropertyValue getPropertyValue(String propertyName) {
    PropertyValue propertyValue = this.propertyValues.get(propertyName);
    if (propertyValue != null) {
        return propertyValue;
    }
    for (PropertySource<?> source : this.propertySources) {
        Object value = source.getProperty(propertyName);
        propertyValue = putIfAbsent(propertyName, value, source);
        if (propertyValue != null) {
            return propertyValue;
        }
    }
    return null;
}
Also used : PropertyValue(org.springframework.beans.PropertyValue)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)41 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 Test (org.junit.Test)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 FieldAccessBean (org.springframework.tests.sample.beans.FieldAccessBean)6 ArrayList (java.util.ArrayList)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2