Java Code Examples for org.springframework.context.annotation.AnnotationBeanNameGenerator

The following examples show how to use org.springframework.context.annotation.AnnotationBeanNameGenerator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: ConfigurationBeanNameTests.java    License: MIT License 6 votes vote down vote up
@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"), is(true));
	assertThat(ctx.containsBean("custom-imported"), is(true));
	assertThat(ctx.containsBean("custom-nested"), is(true));
	assertThat(ctx.containsBean("nestedBean"), is(true));
}
 
Example 2
Source Project: java-technology-stack   Source File: ConfigurationBeanNameTests.java    License: MIT License 6 votes vote down vote up
@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"), is(true));
	assertThat(ctx.containsBean("custom-imported"), is(true));
	assertThat(ctx.containsBean("custom-nested"), is(true));
	assertThat(ctx.containsBean("nestedBean"), is(true));
}
 
Example 3
Source Project: brpc-java   Source File: RpcExporterRegister.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                    BeanDefinitionRegistry registry) {
    Map<Class, String> serviceExporterMap = new HashMap<>();
    AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
    Collection<BeanDefinition> candidates = getCandidates(resourceLoader);
    for (BeanDefinition candidate : candidates) {
        Class<?> clazz = getClass(candidate.getBeanClassName());
        Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(clazz);
        if (interfaces.length != 1) {
            throw new BeanInitializationException("bean interface num must equal 1, " + clazz.getName());
        }
        String serviceBeanName = beanNameGenerator.generateBeanName(candidate, registry);
        String old = serviceExporterMap.putIfAbsent(interfaces[0], serviceBeanName);
        if (old != null) {
            throw new RuntimeException("interface already be exported by bean name:" + old);
        }
        registry.registerBeanDefinition(serviceBeanName, candidate);
    }
}
 
Example 4
@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"), is(true));
	assertThat(ctx.containsBean("custom-imported"), is(true));
	assertThat(ctx.containsBean("custom-nested"), is(true));
	assertThat(ctx.containsBean("nestedBean"), is(true));
}
 
Example 5
@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"), is(true));
}
 
Example 6
@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"), is(true));
}
 
Example 7
@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"), is(true));
}