org.springframework.context.annotation.AnnotationBeanNameGenerator Java Examples

The following examples show how to use org.springframework.context.annotation.AnnotationBeanNameGenerator. 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 File: ConfigurationBeanNameTests.java    From spring-analysis-note with 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 File: ConfigurationBeanNameTests.java    From java-technology-stack with 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 File: RpcExporterRegister.java    From brpc-java with 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
Source File: ConfigurationBeanNameTests.java    From spring4-understanding with Apache License 2.0 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 #5
Source File: AnnotationConfigWebApplicationContextTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
Source File: AnnotationConfigWebApplicationContextTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: AnnotationConfigWebApplicationContextTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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));
}