Search in sources :

Example 1 with ENVIRONMENT_BEAN_NAME

use of org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME in project incubator-dubbo-spring-boot-project by apache.

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 2 with ENVIRONMENT_BEAN_NAME

use of org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME 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)

Aggregations

Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)2 EnableDubboConfig (org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BeanFactoryUtils (org.springframework.beans.factory.BeanFactoryUtils)2 BeanFactoryUtils.beanNamesForTypeIncludingAncestors (org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors)2 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)2 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ApplicationListener (org.springframework.context.ApplicationListener)2 ENVIRONMENT_BEAN_NAME (org.springframework.context.ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME)2 ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)2 Ordered (org.springframework.core.Ordered)2 Environment (org.springframework.core.env.Environment)2