/** * Set the BeanNameGenerator to use for detected bean classes. * <p>The default is a {@link AnnotationBeanNameGenerator}. */ public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) { this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator()); }
@Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { String beanName = super.generateBeanName(definition, registry); return "testing." + beanName; }
@Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { if (definition instanceof AnnotatedBeanDefinition) { String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition); if (StringUtils.hasText(beanName)) { // Explicit bean name found. return beanName; } } // Fallback: generate a unique default bean name. return buildDefaultBeanName(definition, registry); }
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.addBeanFactoryPostProcessor(new BeanDefinitionRegistryPostProcessor() { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { ConstructorArgumentValues cav = new ConstructorArgumentValues(); cav.addGenericArgumentValue(MyClass.class); RootBeanDefinition bean = new RootBeanDefinition(MyFactoryBean.class, cav, null); AnnotationBeanNameGenerator generator = new AnnotationBeanNameGenerator(); registry.registerBeanDefinition(generator.generateBeanName(bean, registry), bean); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } });
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithBlankName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * Derive a default bean name from the given bean definition. * <p>The default implementation delegates to {@link #buildDefaultBeanName(BeanDefinition)}. * @param definition the bean definition to build a bean name for * @param registry the registry that the given bean definition is being registered with * @return the default bean name (never {@code null}) */ protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return buildDefaultBeanName(definition); }
/** * Derive a bean name from one of the annotations on the class. * @param annotatedDef the annotation-aware bean definition * @return the bean name, or {@code null} if none is found */ @Nullable protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Set<String> types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type); if (attributes != null && isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { Object value = attributes.get("value"); if (value instanceof String) { String strVal = (String) value; if (StringUtils.hasLength(strVal)) { if (beanName != null && !strVal.equals(beanName)) { throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'"); } beanName = strVal; } } } } return beanName; }
String beanName = super.determineBeanNameFromAnnotation(annotatedDef); if (beanName != null) { return beanName;
/** * @since 4.0.1 * @see https://jira.spring.io/browse/SPR-11360 */ @Test public void generateBeanNameFromComposedControllerAnnotationWithoutName() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithoutName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
/** * Derive a default bean name from the given bean definition. * <p>The default implementation delegates to {@link #buildDefaultBeanName(BeanDefinition)}. * @param definition the bean definition to build a bean name for * @param registry the registry that the given bean definition is being registered with * @return the default bean name (never {@code null}) */ protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return buildDefaultBeanName(definition); }
/** * Derive a bean name from one of the annotations on the class. * @param annotatedDef the annotation-aware bean definition * @return the bean name, or {@code null} if none is found */ @Nullable protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) { AnnotationMetadata amd = annotatedDef.getMetadata(); Set<String> types = amd.getAnnotationTypes(); String beanName = null; for (String type : types) { AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type); if (attributes != null && isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) { Object value = attributes.get("value"); if (value instanceof String) { String strVal = (String) value; if (StringUtils.hasLength(strVal)) { if (beanName != null && !strVal.equals(beanName)) { throw new IllegalStateException("Stereotype annotations suggest inconsistent " + "component names: '" + beanName + "' versus '" + strVal + "'"); } beanName = strVal; } } } } return beanName; }
@Override protected String determineBeanNameFromAnnotation( AnnotatedBeanDefinition annotatedDef ) { String name = super.determineBeanNameFromAnnotation( annotatedDef ); if ( name == null ) { AnnotationMetadata metadata = annotatedDef.getMetadata(); if ( !metadata.isAnnotated( COMPONENT_ANNOTATION ) || metadata.isAnnotated( CONFIGURATION_ANNOTATION ) ) { return ClassUtils.getPackageName( annotatedDef.getBeanClassName() ) + "." + ClassUtils.getShortName( annotatedDef.getBeanClassName() ); } } return name; } }
/** * Set the BeanNameGenerator to use for detected bean classes. * <p>Default is a {@link AnnotationBeanNameGenerator}. */ public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) { this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator()); }
@Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return "custom-" + super.generateBeanName(definition, registry); } });
@Test public void generateBeanNameWithNamedComponentWhereTheNameIsBlank() { BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.class); String beanName = this.beanNameGenerator.generateBeanName(bd, registry); assertNotNull("The generated beanName must *never* be null.", beanName); assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName)); String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd); assertEquals(expectedGeneratedBeanName, beanName); }
@Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { if (definition instanceof AnnotatedBeanDefinition) { String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition); if (StringUtils.hasText(beanName)) { // Explicit bean name found. return beanName; } } // Fallback: generate a unique default bean name. return buildDefaultBeanName(definition, registry); }
/** * Derive a default bean name from the given bean definition. * <p>The default implementation delegates to {@link #buildDefaultBeanName(BeanDefinition)}. * @param definition the bean definition to build a bean name for * @param registry the registry that the given bean definition is being registered with * @return the default bean name (never {@code null}) */ protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return buildDefaultBeanName(definition); }
protected boolean isStereotypeWithNameValue(String annotationType, Set<String> metaAnnotationTypes, Map<String, Object> attributes) { boolean isStereotype = annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME_ROOT) || annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME_WEB) || annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME_PROXY) || annotationType.equals(COMPONENT_ANNOTATION_CLASSNAME_AUTO) || (metaAnnotationTypes != null && metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME_AUTO)); boolean result = (isStereotype && attributes != null && attributes.containsKey("value")); if (!result) { return super.isStereotypeWithNameValue(annotationType, metaAnnotationTypes, attributes); } else { return result; } }
/** * Set the BeanNameGenerator to use for detected bean classes. * <p>Default is a {@link AnnotationBeanNameGenerator}. */ public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) { this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator()); }
@Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return "custom_" + super.generateBeanName(definition, registry); } }