org.springframework.context.ApplicationContext.findAnnotationOnBean()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(233)

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

ApplicationContext.findAnnotationOnBean介绍

暂无

代码示例

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

/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
  return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class))
      .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null)
      .map(name -> new ControllerAdviceBean(name, context))
      .collect(Collectors.toList());
}

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

/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
  return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class))
      .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null)
      .map(name -> new ControllerAdviceBean(name, context))
      .collect(Collectors.toList());
}

代码示例来源:origin: alibaba/dubbo-spring-boot-starter

private void initProviderBean(String beanName, Object bean) throws Exception {
 Service service = this.applicationContext.findAnnotationOnBean(beanName, Service.class);
 ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service);
 if ((service.interfaceClass() == null || service.interfaceClass() == void.class)

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

private Iterable<Class<?>> getBeanTypesWithAnnotation(Class<? extends Annotation> type) {

    Set<Class<?>> annotatedTypes = new HashSet<>();

    for (String beanName : context.getBeanDefinitionNames()) {

      Annotation annotation = context.findAnnotationOnBean(beanName, type);
      if (annotation != null) {
        annotatedTypes.add(context.getType(beanName));
      }
    }

    return annotatedTypes;
  }
}

代码示例来源:origin: yidongnan/grpc-spring-boot-starter

@Override
public Collection<GrpcCodecDefinition> findGrpcCodecs() {
  if (this.definitions == null) {
    log.debug("Searching for codecs...");
    final String[] beanNames = this.applicationContext.getBeanNamesForAnnotation(GrpcCodec.class);
    final ImmutableList.Builder<GrpcCodecDefinition> builder = ImmutableList.builder();
    for (final String beanName : beanNames) {
      final Codec codec = this.applicationContext.getBean(beanName, Codec.class);
      final GrpcCodec annotation = this.applicationContext.findAnnotationOnBean(beanName, GrpcCodec.class);
      builder.add(new GrpcCodecDefinition(codec, annotation.advertised(), annotation.codecType()));
      log.debug("Found gRPC codec: {}, bean: {}, class: {}",
          codec.getMessageEncoding(), beanName, codec.getClass().getName());
    }
    this.definitions = builder.build();
    log.debug("Done");
  }
  return this.definitions;
}

代码示例来源:origin: yidongnan/grpc-spring-boot-starter

@Override
public Collection<GrpcServiceDefinition> findGrpcServices() {
  Collection<String> beanNames =
      Arrays.asList(this.applicationContext.getBeanNamesForAnnotation(GrpcService.class));
  List<GrpcServiceDefinition> definitions = Lists.newArrayListWithCapacity(beanNames.size());
  GlobalServerInterceptorRegistry globalServerInterceptorRegistry =
      applicationContext.getBean(GlobalServerInterceptorRegistry.class);
  List<ServerInterceptor> globalInterceptorList = globalServerInterceptorRegistry.getServerInterceptors();
  for (String beanName : beanNames) {
    BindableService bindableService = this.applicationContext.getBean(beanName, BindableService.class);
    ServerServiceDefinition serviceDefinition = bindableService.bindService();
    GrpcService grpcServiceAnnotation = applicationContext.findAnnotationOnBean(beanName, GrpcService.class);
    serviceDefinition = bindInterceptors(serviceDefinition, grpcServiceAnnotation, globalInterceptorList);
    definitions.add(new GrpcServiceDefinition(beanName, bindableService.getClass(), serviceDefinition));
    log.debug("Found gRPC service: " + serviceDefinition.getServiceDescriptor().getName() + ", bean: "
        + beanName + ", class: " + bindableService.getClass().getName());
  }
  return definitions;
}

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

/**
 * Extract configuration prefix from {@link ConfigurationProperties} annotation.
 * @param context the application context
 * @param beanFactoryMetaData the bean factory meta-data
 * @param beanName the bean name
 * @return the prefix
 */
private String extractPrefix(ApplicationContext context,
    ConfigurationBeanFactoryMetadata beanFactoryMetaData, String beanName) {
  ConfigurationProperties annotation = context.findAnnotationOnBean(beanName,
      ConfigurationProperties.class);
  if (beanFactoryMetaData != null) {
    ConfigurationProperties override = beanFactoryMetaData
        .findFactoryAnnotation(beanName, ConfigurationProperties.class);
    if (override != null) {
      // The @Bean-level @ConfigurationProperties overrides the one at type
      // level when binding. Arguably we should render them both, but this one
      // might be the most relevant for a starting point.
      annotation = override;
    }
  }
  return annotation.prefix();
}

代码示例来源:origin: com.consol.citrus/citrus-ws

public <A extends Annotation> A findAnnotationOnBean(String beanName,
    Class<A> annotationType) {
  return applicationContext.findAnnotationOnBean(beanName, annotationType);
}
public <T> T getBean(String name, Class<T> requiredType)

代码示例来源:origin: com.github.markash/components-menu

/**
 * You should never need to create instances of this class directly.
 *
 * @param beanName           the name of the bean that implements the view to go to, must not be {@code null}.
 * @param applicationContext the application context to use when looking up beans, must not be {@code null}.
 */
public ViewItemDescriptor(String beanName, ApplicationContext applicationContext) {
  super(beanName, applicationContext);
  this.vaadinView = applicationContext.findAnnotationOnBean(beanName, SpringView.class);
}

代码示例来源:origin: net.javacrumbs/smock-http

public <A extends Annotation> A findAnnotationOnBean(String beanName,
    Class<A> annotationType) {
  return wrappedApplicationContext.findAnnotationOnBean(beanName,
      annotationType);
}

代码示例来源:origin: peholmst/vaadin4spring

/**
 * Attempts to find and return an annotation of the specified type declared on this side bar item.
 *
 * @param annotationType the type of the annotation to look for.
 * @return the annotation, or {@code null} if not found.
 */
public <A extends Annotation> A findAnnotationOnBean(Class<A> annotationType) {
  return applicationContext.findAnnotationOnBean(beanName, annotationType);
}

代码示例来源:origin: peholmst/vaadin4spring

/**
 * You should never need to create instances of this class directly.
 *
 * @param beanName           the name of the bean that implements the view to go to, must not be {@code null}.
 * @param applicationContext the application context to use when looking up beans, must not be {@code null}.
 */
public ViewItemDescriptor(String beanName, ApplicationContext applicationContext) {
  super(beanName, applicationContext);
  this.vaadinView = applicationContext.findAnnotationOnBean(beanName, SpringView.class);
}

代码示例来源:origin: peholmst/vaadin4spring

@Override
  public boolean isAccessGranted(UI ui, String beanName) {
    final SpringView annotation = applicationContext.findAnnotationOnBean(beanName, SpringView.class);
    if (annotation != null) {
      return allowedViews.contains(annotation.name());
    } else {
      return false;
    }
  }
}

代码示例来源:origin: apache/archiva

@Override
public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
  throws NoSuchBeanDefinitionException
{
  return applicationContext.findAnnotationOnBean( s, aClass );
}

代码示例来源:origin: com.airlenet/play-web

public static <T extends Annotation, F> List<F> findAnnotatedBeans(Class<T> annotationType, Class<F> elementType) {
  List<F> beans = new ArrayList<>();
  for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
    if (applicationContext.findAnnotationOnBean(name, annotationType) != null) {
      beans.add(applicationContext.getBean(name, elementType));
    }
  }
  return beans;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-web

/**
 * Find the names of beans annotated with
 * {@linkplain ControllerAdvice @ControllerAdvice} in the given
 * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances.
 */
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) {
  return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class))
      .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null)
      .map(name -> new ControllerAdviceBean(name, context))
      .collect(Collectors.toList());
}

代码示例来源:origin: peholmst/vaadin4spring

protected SideBarItemDescriptor(String beanName, ApplicationContext applicationContext) {
  this.item = applicationContext.findAnnotationOnBean(beanName, SideBarItem.class);
  LOGGER.debug("Item annotation of bean [{}] is [{}]", beanName, item);
  this.i18n = applicationContext.getBean(I18N.class);
  this.applicationContext = applicationContext;
  this.beanName = beanName;
  this.iconAnnotation = findIconAnnotation();
  LOGGER.debug("Icon annotation of bean [{}] is [{}]", beanName, iconAnnotation);
  this.iconProvider = findIconProvider();
  LOGGER.debug("Icon provider of bean [{}] is [{}]", beanName, iconProvider);
}

代码示例来源:origin: cn.home1/oss-lib-swagger-spring-boot-1.4.1.RELEASE

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
 String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(RequestMapping.class);
 Arrays.stream(beanNamesForAnnotation)
   .filter(beanName -> applicationContext.findAnnotationOnBean(beanName, FeignClient.class) != null)
   .forEach(beanName -> throwAnnotationConfigurationException(beanName));
}

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

private Iterable<Class<?>> getBeanTypesWithAnnotation(Class<? extends Annotation> type) {

    Set<Class<?>> annotatedTypes = new HashSet<Class<?>>();

    for (String beanName : context.getBeanDefinitionNames()) {

      Annotation annotation = context.findAnnotationOnBean(beanName, type);
      if (annotation != null) {
        annotatedTypes.add(context.getType(beanName));
      }
    }

    return annotatedTypes;
  }
}

代码示例来源:origin: org.vaadin.spring/spring-vaadin-security

@Override
public boolean isAccessGranted(String beanName, UI ui) {
  Secured viewSecured = applicationContext.findAnnotationOnBean(beanName, Secured.class);
  if ( viewSecured == null ) {
    return true;
  } else if ( security.hasAccessDecisionManager() ) {
    return true; // Leave decision to the second hook
  } else {
    return security.hasAnyAuthority(viewSecured.value());
  }
}

相关文章