Search in sources :

Example 1 with AnnotationBeanNameGenerator

use of org.springframework.context.annotation.AnnotationBeanNameGenerator in project spring-framework by spring-projects.

the class ConfigurationBeanNameTests method registerOuterConfig_withBeanNameGenerator.

@Test
public void registerOuterConfig_withBeanNameGenerator() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() {

        @Override
        public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
            return "custom-" + super.generateBeanName(definition, registry);
        }
    });
    ctx.register(A.class);
    ctx.refresh();
    assertThat(ctx.containsBean("custom-outer")).isTrue();
    assertThat(ctx.containsBean("custom-imported")).isTrue();
    assertThat(ctx.containsBean("custom-nested")).isTrue();
    assertThat(ctx.containsBean("nestedBean")).isTrue();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with AnnotationBeanNameGenerator

use of org.springframework.context.annotation.AnnotationBeanNameGenerator in project spring-framework by spring-projects.

the class AnnotationConfigWebApplicationContextTests method withBeanNameGenerator.

@Test
@SuppressWarnings("resource")
public void withBeanNameGenerator() {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() {

        @Override
        public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
            return "custom-" + super.generateBeanName(definition, registry);
        }
    });
    ctx.setConfigLocation(Config.class.getName());
    ctx.refresh();
    assertThat(ctx.containsBean("custom-myConfig")).isTrue();
}
Also used : BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with AnnotationBeanNameGenerator

use of org.springframework.context.annotation.AnnotationBeanNameGenerator in project dubbo by alibaba.

the class ServiceAnnotationBeanPostProcessor method resolveBeanNameGenerator.

/**
 * It'd better to use BeanNameGenerator instance that should reference
 * {@link ConfigurationClassPostProcessor#componentScanBeanNameGenerator},
 * thus it maybe a potential problem on bean name generation.
 *
 * @param registry {@link BeanDefinitionRegistry}
 * @return {@link BeanNameGenerator} instance
 * @see SingletonBeanRegistry
 * @see AnnotationConfigUtils#CONFIGURATION_BEAN_NAME_GENERATOR
 * @see ConfigurationClassPostProcessor#processConfigBeanDefinitions
 * @since 2.5.8
 */
private BeanNameGenerator resolveBeanNameGenerator(BeanDefinitionRegistry registry) {
    BeanNameGenerator beanNameGenerator = null;
    if (registry instanceof SingletonBeanRegistry) {
        SingletonBeanRegistry singletonBeanRegistry = SingletonBeanRegistry.class.cast(registry);
        beanNameGenerator = (BeanNameGenerator) singletonBeanRegistry.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
    }
    if (beanNameGenerator == null) {
        if (logger.isInfoEnabled()) {
            logger.info("BeanNameGenerator bean can't be found in BeanFactory with name [" + CONFIGURATION_BEAN_NAME_GENERATOR + "]");
            logger.info("BeanNameGenerator will be a instance of " + AnnotationBeanNameGenerator.class.getName() + " , it maybe a potential problem on bean name generation.");
        }
        beanNameGenerator = new AnnotationBeanNameGenerator();
    }
    return beanNameGenerator;
}
Also used : AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) AnnotationBeanNameGenerator(org.springframework.context.annotation.AnnotationBeanNameGenerator) SingletonBeanRegistry(org.springframework.beans.factory.config.SingletonBeanRegistry)

Aggregations

AnnotationBeanNameGenerator (org.springframework.context.annotation.AnnotationBeanNameGenerator)3 Test (org.junit.jupiter.api.Test)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)2 SingletonBeanRegistry (org.springframework.beans.factory.config.SingletonBeanRegistry)1 BeanNameGenerator (org.springframework.beans.factory.support.BeanNameGenerator)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1