Search in sources :

Example 1 with InstantiationAwareBeanPostProcessor

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

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

the class AbstractAutowireCapableBeanFactory method applyBeanPostProcessorsBeforeInstantiation.

/**
	 * Apply InstantiationAwareBeanPostProcessors to the specified bean definition
	 * (by class and name), invoking their {@code postProcessBeforeInstantiation} methods.
	 * <p>Any returned object will be used as the bean instead of actually instantiating
	 * the target bean. A {@code null} return value from the post-processor will
	 * result in the target bean being instantiated.
	 * @param beanClass the class of the bean to be instantiated
	 * @param beanName the name of the bean
	 * @return the bean object to use instead of a default instance of the target bean, or {@code null}
	 * @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation
	 */
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
        if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}
Also used : InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) SmartInstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)

Example 3 with InstantiationAwareBeanPostProcessor

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

the class DefaultListableBeanFactoryTests method doTestFieldSettingWithInstantiationAwarePostProcessor.

private void doTestFieldSettingWithInstantiationAwarePostProcessor(final boolean skipPropertyPopulation) {
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    int ageSetByPropertyValue = 27;
    bd.getPropertyValues().addPropertyValue(new PropertyValue("age", ageSetByPropertyValue));
    lbf.registerBeanDefinition("test", bd);
    final String nameSetOnField = "nameSetOnField";
    lbf.addBeanPostProcessor(new InstantiationAwareBeanPostProcessor() {

        @Override
        public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
            TestBean tb = (TestBean) bean;
            try {
                Field f = TestBean.class.getDeclaredField("name");
                f.setAccessible(true);
                f.set(tb, nameSetOnField);
                return !skipPropertyPopulation;
            } catch (Exception ex) {
                throw new AssertionError("Unexpected exception", ex);
            }
        }
    });
    lbf.preInstantiateSingletons();
    TestBean tb = (TestBean) lbf.getBean("test");
    assertThat(tb.getName()).as("Name was set on field by IAPP").isEqualTo(nameSetOnField);
    if (!skipPropertyPopulation) {
        assertThat(tb.getAge()).as("Property value still set").isEqualTo(ageSetByPropertyValue);
    } else {
        assertThat(tb.getAge()).as("Property value was NOT set and still has default value").isEqualTo(0);
    }
}
Also used : InstantiationAwareBeanPostProcessor(org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor) Field(java.lang.reflect.Field) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertyValue(org.springframework.beans.PropertyValue) ParseException(java.text.ParseException) TypeMismatchException(org.springframework.beans.TypeMismatchException) BeanDefinitionOverrideException(org.springframework.beans.factory.support.BeanDefinitionOverrideException) NotWritablePropertyException(org.springframework.beans.NotWritablePropertyException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MalformedURLException(java.net.MalformedURLException) BeansException(org.springframework.beans.BeansException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) BeansException(org.springframework.beans.BeansException)

Aggregations

InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor)3 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)2 SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 ParseException (java.text.ParseException)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)1 BeansException (org.springframework.beans.BeansException)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 NotWritablePropertyException (org.springframework.beans.NotWritablePropertyException)1 PropertyValue (org.springframework.beans.PropertyValue)1 PropertyValues (org.springframework.beans.PropertyValues)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanDefinitionOverrideException (org.springframework.beans.factory.support.BeanDefinitionOverrideException)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)1 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)1