Search in sources :

Example 36 with PropertyValue

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

the class BeanComponentDefinition method findInnerBeanDefinitionsAndBeanReferences.

private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
    List<BeanDefinition> innerBeans = new ArrayList<>();
    List<BeanReference> references = new ArrayList<>();
    PropertyValues propertyValues = beanDefinition.getPropertyValues();
    for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
        PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
        Object value = propertyValue.getValue();
        if (value instanceof BeanDefinitionHolder) {
            innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());
        } else if (value instanceof BeanDefinition) {
            innerBeans.add((BeanDefinition) value);
        } else if (value instanceof BeanReference) {
            references.add((BeanReference) value);
        }
    }
    this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]);
    this.beanReferences = references.toArray(new BeanReference[references.size()]);
}
Also used : PropertyValues(org.springframework.beans.PropertyValues) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) BeanReference(org.springframework.beans.factory.config.BeanReference) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 37 with PropertyValue

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

the class AbstractAutowireCapableBeanFactory method applyPropertyValues.

/**
	 * Apply the given property values, resolving any runtime references
	 * to other beans in this bean factory. Must use deep copy, so we
	 * don't permanently modify this property.
	 * @param beanName the bean name passed for better exception information
	 * @param mbd the merged bean definition
	 * @param bw the BeanWrapper wrapping the target object
	 * @param pvs the new property values
	 */
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
    if (pvs == null || pvs.isEmpty()) {
        return;
    }
    MutablePropertyValues mpvs = null;
    List<PropertyValue> original;
    if (System.getSecurityManager() != null) {
        if (bw instanceof BeanWrapperImpl) {
            ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext());
        }
    }
    if (pvs instanceof MutablePropertyValues) {
        mpvs = (MutablePropertyValues) pvs;
        if (mpvs.isConverted()) {
            // Shortcut: use the pre-converted values as-is.
            try {
                bw.setPropertyValues(mpvs);
                return;
            } catch (BeansException ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
            }
        }
        original = mpvs.getPropertyValueList();
    } else {
        original = Arrays.asList(pvs.getPropertyValues());
    }
    TypeConverter converter = getCustomTypeConverter();
    if (converter == null) {
        converter = bw;
    }
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
    // Create a deep copy, resolving any references for values.
    List<PropertyValue> deepCopy = new ArrayList<>(original.size());
    boolean resolveNecessary = false;
    for (PropertyValue pv : original) {
        if (pv.isConverted()) {
            deepCopy.add(pv);
        } else {
            String propertyName = pv.getName();
            Object originalValue = pv.getValue();
            Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
            Object convertedValue = resolvedValue;
            boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
            if (convertible) {
                convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
            }
            // in order to avoid re-conversion for every created bean instance.
            if (resolvedValue == originalValue) {
                if (convertible) {
                    pv.setConvertedValue(convertedValue);
                }
                deepCopy.add(pv);
            } else if (convertible && originalValue instanceof TypedStringValue && !((TypedStringValue) originalValue).isDynamic() && !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
                pv.setConvertedValue(convertedValue);
                deepCopy.add(pv);
            } else {
                resolveNecessary = true;
                deepCopy.add(new PropertyValue(pv, convertedValue));
            }
        }
    }
    if (mpvs != null && !resolveNecessary) {
        mpvs.setConverted();
    }
    // Set our (possibly massaged) deep copy.
    try {
        bw.setPropertyValues(new MutablePropertyValues(deepCopy));
    } catch (BeansException ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Error setting property values", ex);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) ArrayList(java.util.ArrayList) PropertyValue(org.springframework.beans.PropertyValue) TypeConverter(org.springframework.beans.TypeConverter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Collection(java.util.Collection) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) BeansException(org.springframework.beans.BeansException)

Example 38 with PropertyValue

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

the class BeanDefinitionParserDelegate method parsePropertyElement.

/**
	 * Parse a property element.
	 */
public void parsePropertyElement(Element ele, BeanDefinition bd) {
    String propertyName = ele.getAttribute(NAME_ATTRIBUTE);
    if (!StringUtils.hasLength(propertyName)) {
        error("Tag 'property' must have a 'name' attribute", ele);
        return;
    }
    this.parseState.push(new PropertyEntry(propertyName));
    try {
        if (bd.getPropertyValues().contains(propertyName)) {
            error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
            return;
        }
        Object val = parsePropertyValue(ele, bd, propertyName);
        PropertyValue pv = new PropertyValue(propertyName, val);
        parseMetaElements(ele, pv);
        pv.setSource(extractSource(ele));
        bd.getPropertyValues().addPropertyValue(pv);
    } finally {
        this.parseState.pop();
    }
}
Also used : PropertyEntry(org.springframework.beans.factory.parsing.PropertyEntry) PropertyValue(org.springframework.beans.PropertyValue)

Example 39 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 40 with PropertyValue

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

the class CacheAnnotationParser method doParse.

private synchronized void doParse(Element element, ParserContext parserContext) {
    String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute("base-package"), ",; \t\n");
    AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);
    if (!parserContext.getRegistry().containsBeanDefinition(CacheAdvisor.CACHE_ADVISOR_BEAN_NAME)) {
        Object eleSource = parserContext.extractSource(element);
        RootBeanDefinition configMapDef = new RootBeanDefinition(ConcurrentHashMap.class);
        configMapDef.setSource(eleSource);
        configMapDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        String configMapName = parserContext.getReaderContext().registerWithGeneratedName(configMapDef);
        RootBeanDefinition interceptorDef = new RootBeanDefinition(JetCacheInterceptor.class);
        interceptorDef.setSource(eleSource);
        interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        interceptorDef.getPropertyValues().addPropertyValue(new PropertyValue("cacheConfigMap", new RuntimeBeanReference(configMapName)));
        String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);
        RootBeanDefinition advisorDef = new RootBeanDefinition(CacheAdvisor.class);
        advisorDef.setSource(eleSource);
        advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        advisorDef.getPropertyValues().addPropertyValue(new PropertyValue("adviceBeanName", interceptorName));
        advisorDef.getPropertyValues().addPropertyValue(new PropertyValue("cacheConfigMap", new RuntimeBeanReference(configMapName)));
        advisorDef.getPropertyValues().addPropertyValue(new PropertyValue("basePackages", basePackages));
        parserContext.getRegistry().registerBeanDefinition(CacheAdvisor.CACHE_ADVISOR_BEAN_NAME, advisorDef);
        CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
        compositeDef.addNestedComponent(new BeanComponentDefinition(configMapDef, configMapName));
        compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
        compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, CacheAdvisor.CACHE_ADVISOR_BEAN_NAME));
        parserContext.registerComponent(compositeDef);
    }
}
Also used : CompositeComponentDefinition(org.springframework.beans.factory.parsing.CompositeComponentDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) BeanComponentDefinition(org.springframework.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference)

Aggregations

PropertyValue (org.springframework.beans.PropertyValue)43 Test (org.junit.Test)16 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 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 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2