Search in sources :

Example 1 with PropertyValues

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

the class AbstractAutowireCapableBeanFactory method populateBean.

/**
	 * Populate the bean instance in the given BeanWrapper with the property values
	 * from the bean definition.
	 * @param beanName the name of the bean
	 * @param mbd the bean definition for the bean
	 * @param bw BeanWrapper with bean instance
	 */
protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
    PropertyValues pvs = mbd.getPropertyValues();
    if (bw == null) {
        if (!pvs.isEmpty()) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
        } else {
            // Skip property population phase for null instance.
            return;
        }
    }
    // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
    // state of the bean before properties are set. This can be used, for example,
    // to support styles of field injection.
    boolean continueWithPropertyPopulation = true;
    if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof InstantiationAwareBeanPostProcessor) {
                InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
                    continueWithPropertyPopulation = false;
                    break;
                }
            }
        }
    }
    if (!continueWithPropertyPopulation) {
        return;
    }
    if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
        MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
        // Add property values based on autowire by name if applicable.
        if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
            autowireByName(beanName, mbd, bw, newPvs);
        }
        // Add property values based on autowire by type if applicable.
        if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
            autowireByType(beanName, mbd, bw, newPvs);
        }
        pvs = newPvs;
    }
    boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
    boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);
    if (hasInstAwareBpps || needsDepCheck) {
        PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
        if (hasInstAwareBpps) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                    if (pvs == null) {
                        return;
                    }
                }
            }
        }
        if (needsDepCheck) {
            checkDependencies(beanName, mbd, filteredPds, pvs);
        }
    }
    applyPropertyValues(beanName, mbd, bw, pvs);
}
Also used : InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyValues(org.springframework.beans.PropertyValues) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyDescriptor(java.beans.PropertyDescriptor) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) MutablePropertyValues(org.springframework.beans.MutablePropertyValues)

Example 2 with PropertyValues

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

the class HttpServletBean method init.

/**
 * Map config parameters onto bean properties of this servlet, and
 * invoke subclass initialization.
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws ServletException {
    // Set bean properties from init parameters.
    PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            if (logger.isErrorEnabled()) {
                logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            }
            throw ex;
        }
    }
    // Let subclasses do whatever initialization they like.
    initServletBean();
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) ResourceEditor(org.springframework.core.io.ResourceEditor) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 3 with PropertyValues

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

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) NestedServletException(org.springframework.web.util.NestedServletException) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Environment(org.springframework.core.env.Environment) ResourceEditor(org.springframework.core.io.ResourceEditor) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 4 with PropertyValues

use of org.springframework.beans.PropertyValues in project jetty.project by eclipse.

the class SpringConfigurationProcessor method init.

@Override
public void init(URL url, XmlParser.Node config, XmlConfiguration configuration) {
    try {
        _configuration = configuration;
        Resource resource = url != null ? new UrlResource(url) : new ByteArrayResource(("" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\" \"http://www.springframework.org/dtd/spring-beans.dtd\">" + config).getBytes(StandardCharsets.UTF_8));
        _beanFactory = new DefaultListableBeanFactory() {

            @Override
            protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
                _configuration.initializeDefaults(bw.getWrappedInstance());
                super.applyPropertyValues(beanName, mbd, bw, pvs);
            }
        };
        new XmlBeanDefinitionReader(_beanFactory).loadBeanDefinitions(resource);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) PropertyValues(org.springframework.beans.PropertyValues) UrlResource(org.springframework.core.io.UrlResource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) UrlResource(org.springframework.core.io.UrlResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ByteArrayResource(org.springframework.core.io.ByteArrayResource) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with PropertyValues

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

the class PropertiesConfigurationFactory method doBindPropertiesToTarget.

private void doBindPropertiesToTarget() throws BindException {
    RelaxedDataBinder dataBinder = (this.targetName != null ? new RelaxedDataBinder(this.target, this.targetName) : new RelaxedDataBinder(this.target));
    if (this.validator != null && this.validator.supports(dataBinder.getTarget().getClass())) {
        dataBinder.setValidator(this.validator);
    }
    if (this.conversionService != null) {
        dataBinder.setConversionService(this.conversionService);
    }
    dataBinder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
    dataBinder.setIgnoreNestedProperties(this.ignoreNestedProperties);
    dataBinder.setIgnoreInvalidFields(this.ignoreInvalidFields);
    dataBinder.setIgnoreUnknownFields(this.ignoreUnknownFields);
    customizeBinder(dataBinder);
    Iterable<String> relaxedTargetNames = getRelaxedTargetNames();
    Set<String> names = getNames(relaxedTargetNames);
    PropertyValues propertyValues = getPropertySourcesPropertyValues(names, relaxedTargetNames);
    dataBinder.bind(propertyValues);
    if (this.validator != null) {
        dataBinder.validate();
    }
    checkForBindingErrors(dataBinder);
}
Also used : PropertyValues(org.springframework.beans.PropertyValues)

Aggregations

PropertyValues (org.springframework.beans.PropertyValues)9 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 BeansException (org.springframework.beans.BeansException)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ResourceEditor (org.springframework.core.io.ResourceEditor)2 ResourceLoader (org.springframework.core.io.ResourceLoader)2 ServletContextResourceLoader (org.springframework.web.context.support.ServletContextResourceLoader)2 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 PropertyValue (org.springframework.beans.PropertyValue)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 BeanReference (org.springframework.beans.factory.config.BeanReference)1 InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor)1 SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1