Search in sources :

Example 41 with PropertyValue

use of org.springframework.beans.PropertyValue 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

PropertyValue (org.springframework.beans.PropertyValue)41 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)16 Test (org.junit.Test)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)8 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 FieldAccessBean (org.springframework.tests.sample.beans.FieldAccessBean)6 ArrayList (java.util.ArrayList)5 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)5 ITestBean (org.springframework.tests.sample.beans.ITestBean)5 TestBean (org.springframework.tests.sample.beans.TestBean)5 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)4 BeanWrapper (org.springframework.beans.BeanWrapper)3 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 BigInteger (java.math.BigInteger)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeansException (org.springframework.beans.BeansException)2