Search in sources :

Example 1 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spock by spockframework.

the class SpringMockTestExecutionListener method beforeTestMethod.

public void beforeTestMethod(TestContext testContext) throws Exception {
    Object testInstance = testContext.getTestInstance();
    if (testInstance instanceof Specification) {
        Specification specification = (Specification) testInstance;
        ScanScopedBeans scanScopedBeans = ReflectionUtil.getAnnotationRecursive(specification.getClass(), ScanScopedBeans.class);
        Set<String> scopes = scanScopedBeans == null ? Collections.<String>emptySet() : new HashSet<String>(Arrays.asList(scanScopedBeans.value()));
        ApplicationContext applicationContext = testContext.getApplicationContext();
        String[] mockBeanNames = applicationContext.getBeanDefinitionNames();
        List<Object> mockedBeans = new ArrayList<Object>();
        for (String beanName : mockBeanNames) {
            BeanDefinition beanDefinition = ((BeanDefinitionRegistry) applicationContext).getBeanDefinition(beanName);
            if (beanDefinition.isAbstract()) {
                continue;
            }
            if (beanDefinition.isSingleton() || scanScopedBean(scanScopedBeans, scopes, beanDefinition)) {
                Object bean = applicationContext.getBean(beanName);
                if (mockUtil.isMock(bean)) {
                    mockUtil.attachMock(bean, specification);
                    mockedBeans.add(bean);
                }
            }
        }
        testContext.setAttribute(MOCKED_BEANS_LIST, mockedBeans);
    } else {
        throw new IllegalArgumentException("SpringMockTestExecutionListener is only applicable for spock specifications.");
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Specification(spock.lang.Specification) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext)

Example 2 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-boot by spring-projects.

the class EndpointWebMvcAutoConfiguration method registerServletWebServerFactory.

private void registerServletWebServerFactory(AnnotationConfigServletWebServerApplicationContext childContext) {
    try {
        ConfigurableListableBeanFactory beanFactory = childContext.getBeanFactory();
        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            registry.registerBeanDefinition("ServletWebServerFactory", new RootBeanDefinition(determineServletWebServerFactoryClass()));
        }
    } catch (NoSuchBeanDefinitionException ex) {
    // Ignore and assume auto-configuration
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 3 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-security by spring-projects.

the class MessageSecurityPostProcessorTest method handlesBeansWithoutClass.

@Test
public void handlesBeansWithoutClass() {
    BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
    registry.registerBeanDefinition("beanWithoutClass", new GenericBeanDefinition());
    postProcessor.postProcessBeanDefinitionRegistry(registry);
}
Also used : SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SimpleBeanDefinitionRegistry(org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Test(org.junit.Test)

Example 4 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project spring-framework by spring-projects.

the class PostProcessorRegistrationDelegate method invokeBeanFactoryPostProcessors.

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
    // Invoke BeanDefinitionRegistryPostProcessors first, if any.
    Set<String> processedBeans = new HashSet<>();
    if (beanFactory instanceof BeanDefinitionRegistry) {
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<>();
        List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<>();
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
                registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
                registryPostProcessors.add(registryPostProcessor);
            } else {
                regularPostProcessors.add(postProcessor);
            }
        }
        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        // Separate between BeanDefinitionRegistryPostProcessors that implement
        // PriorityOrdered, Ordered, and the rest.
        String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
        List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
        for (String ppName : postProcessorNames) {
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
        registryPostProcessors.addAll(priorityOrderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
        // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<>();
        for (String ppName : postProcessorNames) {
            if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        sortPostProcessors(beanFactory, orderedPostProcessors);
        registryPostProcessors.addAll(orderedPostProcessors);
        invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
        // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
        boolean reiterate = true;
        while (reiterate) {
            reiterate = false;
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName)) {
                    BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
                    registryPostProcessors.add(pp);
                    processedBeans.add(ppName);
                    pp.postProcessBeanDefinitionRegistry(registry);
                    reiterate = true;
                }
            }
        }
        // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
        invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    } else {
        // Invoke factory processors registered with the context instance.
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }
    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let the bean factory post-processors apply to them!
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
        // skip - already processed in first phase above
        } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        } else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }
    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>();
    for (String postProcessorName : orderedPostProcessorNames) {
        orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    sortPostProcessors(beanFactory, orderedPostProcessors);
    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
    // Finally, invoke all other BeanFactoryPostProcessors.
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
    // Clear cached merged bean definitions since the post-processors might have
    // modified the original metadata, e.g. replacing placeholders in values...
    beanFactory.clearMetadataCache();
}
Also used : ArrayList(java.util.ArrayList) Ordered(org.springframework.core.Ordered) PriorityOrdered(org.springframework.core.PriorityOrdered) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) BeanDefinitionRegistryPostProcessor(org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor) LinkedList(java.util.LinkedList) PriorityOrdered(org.springframework.core.PriorityOrdered) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) HashSet(java.util.HashSet)

Example 5 with BeanDefinitionRegistry

use of org.springframework.beans.factory.support.BeanDefinitionRegistry in project dubbo by alibaba.

the class AnnotationBean method postProcessBeanFactory.

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (annotationPackage == null || annotationPackage.length() == 0) {
        return;
    }
    if (beanFactory instanceof BeanDefinitionRegistry) {
        try {
            // init scanner
            Class<?> scannerClass = ReflectUtils.forName("org.springframework.context.annotation.ClassPathBeanDefinitionScanner");
            Object scanner = scannerClass.getConstructor(new Class<?>[] { BeanDefinitionRegistry.class, boolean.class }).newInstance(new Object[] { (BeanDefinitionRegistry) beanFactory, true });
            // add filter
            Class<?> filterClass = ReflectUtils.forName("org.springframework.core.type.filter.AnnotationTypeFilter");
            Object filter = filterClass.getConstructor(Class.class).newInstance(Service.class);
            Method addIncludeFilter = scannerClass.getMethod("addIncludeFilter", ReflectUtils.forName("org.springframework.core.type.filter.TypeFilter"));
            addIncludeFilter.invoke(scanner, filter);
            // scan packages
            String[] packages = Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);
            Method scan = scannerClass.getMethod("scan", new Class<?>[] { String[].class });
            scan.invoke(scanner, new Object[] { packages });
        } catch (Throwable e) {
        // spring 2.0
        }
    }
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Method(java.lang.reflect.Method)

Aggregations

BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)81 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)33 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)20 Test (org.junit.jupiter.api.Test)15 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)13 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)12 SimpleBeanDefinitionRegistry (org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry)11 Map (java.util.Map)9 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)9 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)8 ApplicationContext (org.springframework.context.ApplicationContext)8 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)6 CompositeComponentDefinition (org.springframework.beans.factory.parsing.CompositeComponentDefinition)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)4 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)4 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4