Search in sources :

Example 46 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project spring-boot by spring-projects.

the class PropertyMappingContextCustomizerFactoryTests method getContextCustomizerWhenHasNoMappingShouldNotAddPropertySource.

@Test
void getContextCustomizerWhenHasNoMappingShouldNotAddPropertySource() {
    ContextCustomizer customizer = this.factory.createContextCustomizer(NoMapping.class, null);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class);
    ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
    given(context.getEnvironment()).willReturn(environment);
    given(context.getBeanFactory()).willReturn(beanFactory);
    customizer.customizeContext(context, null);
    then(environment).shouldHaveNoInteractions();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ContextCustomizer(org.springframework.test.context.ContextCustomizer) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 47 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project dubbo by alibaba.

the class DubboConfigBeanDefinitionConflictApplicationListener method resolveUniqueApplicationConfigBean.

/**
 * Resolve the unique {@link ApplicationConfig} Bean
 * @param registry {@link BeanDefinitionRegistry} instance
 * @param beanFactory {@link ConfigurableListableBeanFactory} instance
 * @see EnableDubboConfig
 */
private void resolveUniqueApplicationConfigBean(BeanDefinitionRegistry registry, ListableBeanFactory beanFactory) {
    String[] beansNames = beanNamesForTypeIncludingAncestors(beanFactory, ApplicationConfig.class);
    if (beansNames.length < 2) {
        // If the number of ApplicationConfig beans is less than two, return immediately.
        return;
    }
    Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
    // Remove ApplicationConfig Beans that are configured by "dubbo.application.*"
    Stream.of(beansNames).filter(beansName -> isConfiguredApplicationConfigBeanName(environment, beansName)).forEach(registry::removeBeanDefinition);
    beansNames = beanNamesForTypeIncludingAncestors(beanFactory, ApplicationConfig.class);
    if (beansNames.length > 1) {
        throw new IllegalStateException(String.format("There are more than one instances of %s, whose bean definitions : %s", ApplicationConfig.class.getSimpleName(), Stream.of(beansNames).map(registry::getBeanDefinition).collect(Collectors.toList())));
    }
}
Also used : Ordered(org.springframework.core.Ordered) Logger(org.slf4j.Logger) ENVIRONMENT_BEAN_NAME(org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) LoggerFactory(org.slf4j.LoggerFactory) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) ApplicationListener(org.springframework.context.ApplicationListener) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Objects(java.util.Objects) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Stream(java.util.stream.Stream) Environment(org.springframework.core.env.Environment) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) EnableDubboConfig(org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig) BeanFactoryUtils.beanNamesForTypeIncludingAncestors(org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) Environment(org.springframework.core.env.Environment)

Example 48 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project dubbo by alibaba.

the class DubboConfigEarlyInitializationTest method testDubboConfigEarlyInitializationPostProcessor.

@Test
public void testDubboConfigEarlyInitializationPostProcessor() {
    assertTrue(applicationContext instanceof GenericApplicationContext);
    ConfigurableListableBeanFactory clBeanFactory = ((GenericApplicationContext) applicationContext).getBeanFactory();
    assertTrue(clBeanFactory instanceof DefaultListableBeanFactory);
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) clBeanFactory;
    List<BeanPostProcessor> beanPostProcessorList = beanFactory.getBeanPostProcessors();
    assertEquals(beanFactory.getBeanPostProcessorCount(), beanPostProcessorList.size());
    boolean containsDubboConfigEarlyInitializationPostProcessor = false;
    for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
        if (beanPostProcessor instanceof DubboConfigEarlyRegistrationPostProcessor.DubboConfigEarlyInitializationPostProcessor) {
            containsDubboConfigEarlyInitializationPostProcessor = true;
            break;
        }
    }
    assertTrue(containsDubboConfigEarlyInitializationPostProcessor);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 49 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project engine by craftercms.

the class BeanDefinitionUtils method createBeanDefinitionFromOriginal.

/**
 * Creates a bean definition for the specified bean name. If the parent context of the current context contains a
 * bean definition with the same name, the definition is created as a bean copy of the parent definition. This
 * method is useful for config parsers that want to create a bean definition from configuration but also want to
 * retain the default properties of the original bean.
 *
 * @param applicationContext    the current application context
 * @param beanName
 * @return the bean definition
 */
public static BeanDefinition createBeanDefinitionFromOriginal(ApplicationContext applicationContext, String beanName) {
    ApplicationContext parentContext = applicationContext.getParent();
    BeanDefinition parentDefinition = null;
    if (parentContext != null && parentContext.getAutowireCapableBeanFactory() instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory parentBeanFactory = (ConfigurableListableBeanFactory) parentContext.getAutowireCapableBeanFactory();
        try {
            parentDefinition = parentBeanFactory.getBeanDefinition(beanName);
        } catch (NoSuchBeanDefinitionException e) {
        }
    }
    if (parentDefinition != null) {
        return new GenericBeanDefinition(parentDefinition);
    } else {
        return new GenericBeanDefinition();
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) ApplicationContext(org.springframework.context.ApplicationContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 50 with ConfigurableListableBeanFactory

use of org.springframework.beans.factory.config.ConfigurableListableBeanFactory in project grails-core by grails.

the class DefaultGrailsPlugin method invokeOnChangeListener.

private void invokeOnChangeListener(Map event) {
    onChangeListener.setDelegate(this);
    onChangeListener.call(new Object[] { event });
    if (!(applicationContext instanceof GenericApplicationContext)) {
        return;
    }
    // Apply any factory post processors in case the change listener has changed any
    // bean definitions (GRAILS-5763)
    GenericApplicationContext ctx = (GenericApplicationContext) applicationContext;
    ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
    for (BeanFactoryPostProcessor postProcessor : ctx.getBeanFactoryPostProcessors()) {
        try {
            postProcessor.postProcessBeanFactory(beanFactory);
        } catch (IllegalStateException e) {
        // post processor doesn't allow running again, just continue
        }
    }
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor)

Aggregations

ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)104 Test (org.junit.Test)16 Test (org.junit.jupiter.api.Test)15 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)13 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)12 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)10 ApplicationContext (org.springframework.context.ApplicationContext)9 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)8 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)7 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)7 Map (java.util.Map)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)6 Collectors (java.util.stream.Collectors)5 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)5 HashMap (java.util.HashMap)4 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)4 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)4