Search in sources :

Example 1 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor 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 BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method determineConstructorsFromBeanPostProcessors.

/**
	 * Determine candidate constructors to use for the given bean, checking all registered
	 * {@link SmartInstantiationAwareBeanPostProcessor SmartInstantiationAwareBeanPostProcessors}.
	 * @param beanClass the raw class of the bean
	 * @param beanName the name of the bean
	 * @return the candidate constructors, or {@code null} if none specified
	 * @throws org.springframework.beans.BeansException in case of errors
	 * @see org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor#determineCandidateConstructors
	 */
protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(Class<?> beanClass, String beanName) throws BeansException {
    if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName);
                if (ctors != null) {
                    return ctors;
                }
            }
        }
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)

Example 3 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method getEarlyBeanReference.

/**
	 * Obtain a reference for early access to the specified bean,
	 * typically for the purpose of resolving a circular reference.
	 * @param beanName the name of the bean (for error handling purposes)
	 * @param mbd the merged bean definition for the bean
	 * @param bean the raw bean instance
	 * @return the object to expose as bean reference
	 */
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
    Object exposedObject = bean;
    if (bean != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
                if (exposedObject == null) {
                    return null;
                }
            }
        }
    }
    return exposedObject;
}
Also used : BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)

Example 4 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class AbstractAutowireCapableBeanFactory method applyMergedBeanDefinitionPostProcessors.

/**
	 * Apply MergedBeanDefinitionPostProcessors to the specified bean definition,
	 * invoking their {@code postProcessMergedBeanDefinition} methods.
	 * @param mbd the merged bean definition for the bean
	 * @param beanType the actual type of the managed bean instance
	 * @param beanName the name of the bean
	 * @see MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition
	 */
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
        if (bp instanceof MergedBeanDefinitionPostProcessor) {
            MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
            bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
        }
    }
}
Also used : BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)

Example 5 with BeanPostProcessor

use of org.springframework.beans.factory.config.BeanPostProcessor in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testBeanPostProcessorWithWrappedObjectAndDisposableBean.

@Test
public void testBeanPostProcessorWithWrappedObjectAndDisposableBean() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return new TestBean();
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    });
    BeanWithDisposableBean.closed = false;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertTrue("Destroy method invoked", BeanWithDisposableBean.closed);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)20 InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)7 SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)6 Test (org.junit.jupiter.api.Test)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)4 TestBean (org.springframework.beans.testfixture.beans.TestBean)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 Test (org.junit.Test)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)3 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)3 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)3 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)3 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2