Search in sources :

Example 6 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)

Example 7 with BeanPostProcessor

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

the class DefaultListableBeanFactoryTests method testBeanPostProcessorWithWrappedObjectAndDestroyMethod.

@Test
public void testBeanPostProcessorWithWrappedObjectAndDestroyMethod() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class);
    bd.setDestroyMethodName("close");
    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;
        }
    });
    BeanWithDestroyMethod.closeCount = 0;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertEquals("Destroy methods invoked", 1, BeanWithDestroyMethod.closeCount);
}
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)

Example 8 with BeanPostProcessor

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

the class XmlWebApplicationContextTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    InitAndIB.constructed = false;
    root = new XmlWebApplicationContext();
    root.getEnvironment().addActiveProfile("rootProfile1");
    MockServletContext sc = new MockServletContext("");
    root.setServletContext(sc);
    root.setConfigLocations(new String[] { "/org/springframework/web/context/WEB-INF/applicationContext.xml" });
    root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.addBeanPostProcessor(new BeanPostProcessor() {

                @Override
                public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
                    if (bean instanceof TestBean) {
                        ((TestBean) bean).getFriends().add("myFriend");
                    }
                    return bean;
                }

                @Override
                public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
                    return bean;
                }
            });
        }
    });
    root.refresh();
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.getEnvironment().addActiveProfile("wacProfile1");
    wac.setParent(root);
    wac.setServletContext(sc);
    wac.setNamespace("test-servlet");
    wac.setConfigLocations(new String[] { "/org/springframework/web/context/WEB-INF/test-servlet.xml" });
    wac.refresh();
    return wac;
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 9 with BeanPostProcessor

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

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

the class AbstractAutowireCapableBeanFactory method predictBeanType.

@Override
protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
    Class<?> targetType = determineTargetType(beanName, mbd, typesToMatch);
    // eventual type after a before-instantiation shortcut.
    if (targetType != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                Class<?> predicted = ibp.predictBeanType(targetType, beanName);
                if (predicted != null && (typesToMatch.length != 1 || FactoryBean.class != typesToMatch[0] || FactoryBean.class.isAssignableFrom(predicted))) {
                    return predicted;
                }
            }
        }
    }
    return targetType;
}
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) FactoryBean(org.springframework.beans.factory.FactoryBean)

Aggregations

BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)14 InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor)6 SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 Test (org.junit.Test)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)3 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 AopInfrastructureBean (org.springframework.aop.framework.AopInfrastructureBean)1 BeansException (org.springframework.beans.BeansException)1 PropertyValues (org.springframework.beans.PropertyValues)1