org.springframework.context.annotation.AnnotationBeanNameGenerator类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(147)

本文整理了Java中org.springframework.context.annotation.AnnotationBeanNameGenerator类的一些代码示例,展示了AnnotationBeanNameGenerator类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AnnotationBeanNameGenerator类的具体详情如下:
包路径:org.springframework.context.annotation.AnnotationBeanNameGenerator
类名称:AnnotationBeanNameGenerator

AnnotationBeanNameGenerator介绍

[英]org.springframework.beans.factory.support.BeanNameGeneratorimplementation for bean classes annotated with the org.springframework.stereotype.Component annotation or with another annotation that is itself annotated with org.springframework.stereotype.Component as a meta-annotation. For example, Spring's stereotype annotations (such as org.springframework.stereotype.Repository) are themselves annotated with org.springframework.stereotype.Component.

Also supports Java EE 6's javax.annotation.ManagedBean and JSR-330's javax.inject.Named annotations, if available. Note that Spring component annotations always override such standard annotations.

If the annotation's value doesn't indicate a bean name, an appropriate name will be built based on the short name of the class (with the first letter lower-cased). For example:

com.xyz.FooServiceImpl -> fooServiceImpl

[中]组织。springframework。豆。工厂支持用org注释的bean类的BeanNameGenerator实现。springframework。刻板印象组件注释或其他注释,该注释本身由org注释。springframework。刻板印象组件作为元注释。例如,Spring的原型注释(例如org.springframework.stereotype.Repository)本身就是用org注释的。springframework。刻板印象组成部分
还支持JavaEE6的javax。注释。ManagedBean和JSR-330的javax。注射命名注释(如果可用)。请注意,Spring组件注释始终覆盖此类标准注释。
如果注释的值不表示bean名称,则将基于类的短名称(第一个字母小写)生成适当的名称。例如:

com.xyz.FooServiceImpl -> fooServiceImpl

代码示例

代码示例来源:origin: spring-projects/spring-framework

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

代码示例来源:origin: spring-projects/spring-framework

@Override
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
  String beanName = super.generateBeanName(definition, registry);
  return "testing." + beanName;
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: stackoverflow.com

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 { }
 });

代码示例来源:origin: spring-projects/spring-framework

/**
 * @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);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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;
}

代码示例来源:origin: org.talend.daikon/daikon-spring-utils

String beanName = super.determineBeanNameFromAnnotation(annotatedDef);
if (beanName != null) {
  return beanName;

代码示例来源:origin: spring-projects/spring-framework

/**
 * @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);
}

代码示例来源:origin: org.springframework/spring-context

/**
 * 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);
}

代码示例来源:origin: org.springframework/spring-context

/**
 * 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;
}

代码示例来源:origin: com.foreach.across/across-core

@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;
  }
}

代码示例来源:origin: spring-projects/spring-framework

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

代码示例来源:origin: spring-projects/spring-framework

@Override
  public String generateBeanName(BeanDefinition definition,
      BeanDefinitionRegistry registry) {
    return "custom-" + super.generateBeanName(definition, registry);
  }
});

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: org.springframework/spring-context

@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);
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * 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);
}

代码示例来源:origin: top.wboost/common-context

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;
  }
}

代码示例来源:origin: org.springframework/spring-context

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

代码示例来源:origin: spring-projects/spring-framework

@Override
  public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    return "custom_" + super.generateBeanName(definition, registry);
  }
}

相关文章