Search in sources :

Example 6 with BeanFactoryPostProcessor

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

the class FactoryBeanTests method testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure.

@Test
public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throws Exception {
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(factory).loadBeanDefinitions(WITH_AUTOWIRING_CONTEXT);
    BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
    ppc.postProcessBeanFactory(factory);
    Beta beta = (Beta) factory.getBean("beta");
    Alpha alpha = (Alpha) factory.getBean("alpha");
    Gamma gamma = (Gamma) factory.getBean("gamma");
    assertSame(beta, alpha.getBeta());
    assertSame(gamma, beta.getGamma());
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) Test(org.junit.Test)

Example 7 with BeanFactoryPostProcessor

use of org.springframework.beans.factory.config.BeanFactoryPostProcessor in project ignite by apache.

the class IgniteSpringHelperImpl method prepareSpringContext.

/**
     * Prepares Spring context.
     *
     * @param excludedProps Properties to be excluded.
     * @return application context.
     */
private static GenericApplicationContext prepareSpringContext(final String... excludedProps) {
    GenericApplicationContext springCtx = new GenericApplicationContext();
    if (excludedProps.length > 0) {
        final List<String> excludedPropsList = Arrays.asList(excludedProps);
        BeanFactoryPostProcessor postProc = new BeanFactoryPostProcessor() {

            /**
                 * @param def Registered BeanDefinition.
                 * @throws BeansException in case of errors.
                 */
            private void processNested(BeanDefinition def) throws BeansException {
                Iterator<PropertyValue> iterVals = def.getPropertyValues().getPropertyValueList().iterator();
                while (iterVals.hasNext()) {
                    PropertyValue val = iterVals.next();
                    if (excludedPropsList.contains(val.getName())) {
                        iterVals.remove();
                        continue;
                    }
                    if (val.getValue() instanceof Iterable) {
                        Iterator iterNested = ((Iterable) val.getValue()).iterator();
                        while (iterNested.hasNext()) {
                            Object item = iterNested.next();
                            if (item instanceof BeanDefinitionHolder) {
                                BeanDefinitionHolder h = (BeanDefinitionHolder) item;
                                try {
                                    if (h.getBeanDefinition().getBeanClassName() != null)
                                        Class.forName(h.getBeanDefinition().getBeanClassName());
                                    processNested(h.getBeanDefinition());
                                } catch (ClassNotFoundException ignored) {
                                    iterNested.remove();
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                for (String beanName : beanFactory.getBeanDefinitionNames()) {
                    try {
                        BeanDefinition def = beanFactory.getBeanDefinition(beanName);
                        if (def.getBeanClassName() != null)
                            Class.forName(def.getBeanClassName());
                        processNested(def);
                    } catch (ClassNotFoundException ignored) {
                        ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition(beanName);
                    }
                }
            }
        };
        springCtx.addBeanFactoryPostProcessor(postProc);
    }
    return springCtx;
}
Also used : PropertyValue(org.springframework.beans.PropertyValue) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) Iterator(java.util.Iterator) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)7 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)4 Test (org.junit.Test)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 BeansException (org.springframework.beans.BeansException)1 PropertyValue (org.springframework.beans.PropertyValue)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)1 BeanDefinitionRegistryPostProcessor (org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor)1 Ordered (org.springframework.core.Ordered)1 PriorityOrdered (org.springframework.core.PriorityOrdered)1 MockServletContext (org.springframework.mock.web.test.MockServletContext)1