Example usage for org.springframework.beans.factory.config TypedStringValue getTargetType

List of usage examples for org.springframework.beans.factory.config TypedStringValue getTargetType

Introduction

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

Prototype

public Class<?> getTargetType() 

Source Link

Document

Return the type to convert to.

Usage

From source file:de.acosix.alfresco.mtsupport.repo.subsystems.TenantAwareSubsystemPlaceholderConfigurer.java

/**
 * {@inheritDoc}/*ww  w. ja  v a  2  s  .c o m*/
 */
@Override
protected void doProcessProperties(final ConfigurableListableBeanFactory beanFactoryToProcess,
        final StringValueResolver valueResolver) {
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Object resolveValue(final Object value) {
            Object result = value;

            // TODO Report bug with Spring
            // TypedStringValue may be reused as result of cloneBeanDefinition and thus should not be mutated

            if (value instanceof TypedStringValue) {
                final TypedStringValue typedStringValue = (TypedStringValue) value;
                final String stringValue = typedStringValue.getValue();
                if (stringValue != null) {
                    final String visitedString = this.resolveStringValue(stringValue);
                    if (!stringValue.equals(visitedString)) {
                        result = typedStringValue.hasTargetType()
                                ? new TypedStringValue(visitedString, typedStringValue.getTargetType())
                                : new TypedStringValue(visitedString);
                    }
                }
            } else {
                result = super.resolveValue(value);
            }

            return result;
        }
    };

    final String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (final String curName : beanNames) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            final String tenantDomain;
            if (curName.contains(TenantBeanUtils.TENANT_BEAN_NAME_PATTERN)) {
                tenantDomain = curName.substring(curName.indexOf(TenantBeanUtils.TENANT_BEAN_NAME_PATTERN)
                        + TenantBeanUtils.TENANT_BEAN_NAME_PATTERN.length());
                LOGGER.debug("[{}] Processing bean {} for tenant domain {}", this.beanName, curName,
                        tenantDomain);
            } else {
                LOGGER.debug("[{}] Processing bean {} without tenant domain", this.beanName, curName);
                tenantDomain = null;
            }

            final BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            TENANT_CONTEXT.set(tenantDomain);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (final Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage());
            } finally {
                TENANT_CONTEXT.remove();
            }
        }
    }
    LOGGER.debug("[{}] Completed processing all beans", this.beanName);

    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);

    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);

}

From source file:org.springframework.beans.factory.support.BeanDefinitionValueResolver.java

/**
 * Given a PropertyValue, return a value, resolving any references to other
 * beans in the factory if necessary. The value could be:
 * <li>A BeanDefinition, which leads to the creation of a corresponding
 * new bean instance. Singleton flags and names of such "inner beans"
 * are always ignored: Inner beans are anonymous prototypes.
 * <li>A RuntimeBeanReference, which must be resolved.
 * <li>A ManagedList. This is a special collection that may contain
 * RuntimeBeanReferences or Collections that will need to be resolved.
 * <li>A ManagedSet. May also contain RuntimeBeanReferences or
 * Collections that will need to be resolved.
 * <li>A ManagedMap. In this case the value may be a RuntimeBeanReference
 * or Collection that will need to be resolved.
 * <li>An ordinary object or <code>null</code>, in which case it's left alone.
 *///from w  w w . jav a2s. c  o m
public Object resolveValueIfNecessary(String argName, Object value) throws BeansException {
    // We must check each value to see whether it requires a runtime reference
    // to another bean to be resolved.
    if (value instanceof BeanDefinitionHolder) {
        // Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases.
        BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value;
        return resolveInnerBeanDefinition(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition());
    } else if (value instanceof BeanDefinition) {
        // Resolve plain BeanDefinition, without contained name: use dummy name.
        BeanDefinition bd = (BeanDefinition) value;
        return resolveInnerBeanDefinition(argName, "(inner bean)", bd);
    } else if (value instanceof RuntimeBeanReference) {
        RuntimeBeanReference ref = (RuntimeBeanReference) value;
        return resolveReference(argName, ref);
    } else if (value instanceof ManagedList) {
        // May need to resolve contained runtime references.
        return resolveManagedList(argName, (List) value);
    } else if (value instanceof ManagedSet) {
        // May need to resolve contained runtime references.
        return resolveManagedSet(argName, (Set) value);
    } else if (value instanceof ManagedMap) {
        // May need to resolve contained runtime references.
        return resolveManagedMap(argName, (Map) value);
    } else if (value instanceof TypedStringValue) {
        // Convert value to target type here.
        TypedStringValue typedStringValue = (TypedStringValue) value;
        try {
            return this.beanFactory.doTypeConversionIfNecessary(typedStringValue.getValue(),
                    typedStringValue.getTargetType());
        } catch (Throwable ex) {
            // Improve the message by showing the context.
            throw new BeanCreationException(this.beanDefinition.getResourceDescription(), this.beanName,
                    "Error converting typed String value for " + argName, ex);
        }
    } else {
        // No need to resolve value...
        return value;
    }
}