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();
}
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();
}
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;
}
Aggregations