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

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

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

ApplicationContext.getBeanNamesForAnnotation介绍

暂无

代码示例

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

/**
 * Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}.
 */
protected void registerEndpoints() {
  Set<Class<?>> endpointClasses = new LinkedHashSet<>();
  if (this.annotatedEndpointClasses != null) {
    endpointClasses.addAll(this.annotatedEndpointClasses);
  }
  ApplicationContext context = getApplicationContext();
  if (context != null) {
    String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class);
    for (String beanName : endpointBeanNames) {
      endpointClasses.add(context.getType(beanName));
    }
  }
  for (Class<?> endpointClass : endpointClasses) {
    registerEndpoint(endpointClass);
  }
  if (context != null) {
    Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class);
    for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) {
      registerEndpoint(endpointConfig);
    }
  }
}

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

@Override
public void addServerInterceptors(final GlobalServerInterceptorRegistry registry) {
  final String[] names = this.context.getBeanNamesForAnnotation(GrpcGlobalServerInterceptor.class);
  for (final String name : names) {
    final ServerInterceptor interceptor = this.context.getBean(name, ServerInterceptor.class);
    log.debug("Registering GlobalServerInterceptor: {} ({})", name, interceptor);
    registry.addServerInterceptors(interceptor);
  }
}

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

@Override
public void addClientInterceptors(final GlobalClientInterceptorRegistry registry) {
  final String[] names = this.context.getBeanNamesForAnnotation(GrpcGlobalClientInterceptor.class);
  for (final String name : names) {
    final ClientInterceptor interceptor = this.context.getBean(name, ClientInterceptor.class);
    log.debug("Registering GlobalClientInterceptor: {} ({})", name, interceptor);
    registry.addClientInterceptors(interceptor);
  }
}

代码示例来源:origin: foxinmy/weixin4j

/**
 * 获取所有的handler
 *
 * @return handler集合
 * @throws WeixinException
 * @see com.zone.weixin4j.handler.WeixinMessageHandler
 */
public WeixinMessageHandler[] getMessageHandlers() throws WeixinException {
  if (this.messageHandlers == null) {
    String[] beanNamesForAnnotation = this.weiXin4jContextAware.getApplicationContext().getBeanNamesForAnnotation(WxMessageHandler.class);
    for (String str : beanNamesForAnnotation) {
      Object bean = this.weiXin4jContextAware.getApplicationContext().getBean(str);
      if (bean instanceof WeixinMessageHandler) {
        this.messageHandlerList.add((WeixinMessageHandler) this.weiXin4jContextAware.getApplicationContext().getBean(str));
      }
    }
  }
  if (messageHandlerList != null && !this.messageHandlerList.isEmpty()) {
    this.messageHandlers = this.messageHandlerList.toArray(new WeixinMessageHandler[this.messageHandlerList.size()]);
  }
  return this.messageHandlers;
}

代码示例来源: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: foxinmy/weixin4j

/**
 * 获取所有的interceptor
 *
 * @return interceptor集合
 * @throws WeixinException
 */
public WeixinMessageInterceptor[] getMessageInterceptors()
    throws WeixinException {
  if (this.messageInterceptors == null) {
    String[] beanNamesForAnnotation = this.weiXin4jContextAware.getApplicationContext().getBeanNamesForAnnotation(WxMessageInterceptor.class);
    for (String str : beanNamesForAnnotation) {
      Object bean = this.weiXin4jContextAware.getApplicationContext().getBean(str);
      if (bean instanceof WeixinMessageInterceptor) {
        this.messageInterceptorList.add((WeixinMessageInterceptor) this.weiXin4jContextAware.getApplicationContext().getBean(str));
      }
    }
  }
  if (this.messageInterceptorList != null && !this.messageInterceptorList.isEmpty()) {
    Collections.sort(messageInterceptorList,
        new Comparator<WeixinMessageInterceptor>() {
          @Override
          public int compare(WeixinMessageInterceptor m1, WeixinMessageInterceptor m2) {
            return m2.weight() - m1.weight();
          }
        });
    this.messageInterceptors = this.messageInterceptorList.toArray(new WeixinMessageInterceptor[this.messageInterceptorList.size()]);
  }
  logger.info(String.format("resolve message interceptors %s", this.messageInterceptorList));
  return this.messageInterceptors;
}

代码示例来源: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: unic/neba

@Override
public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType) {
  return wrapped.getBeanNamesForAnnotation(annotationType);
}

代码示例来源:origin: net.devh/grpc-client-spring-boot-autoconfigure

@Override
public void addClientInterceptors(final GlobalClientInterceptorRegistry registry) {
  final String[] names = this.context.getBeanNamesForAnnotation(GrpcGlobalClientInterceptor.class);
  for (final String name : names) {
    final ClientInterceptor interceptor = this.context.getBean(name, ClientInterceptor.class);
    log.debug("Registering GlobalClientInterceptor: {} ({})", name, interceptor);
    registry.addClientInterceptors(interceptor);
  }
}

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

@Override
public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
{
  return applicationContext.getBeanNamesForAnnotation( aClass );
}

代码示例来源:origin: net.devh/grpc-server-spring-boot-autoconfigure

@Override
public void addServerInterceptors(final GlobalServerInterceptorRegistry registry) {
  final String[] names = this.context.getBeanNamesForAnnotation(GrpcGlobalServerInterceptor.class);
  for (final String name : names) {
    final ServerInterceptor interceptor = this.context.getBean(name, ServerInterceptor.class);
    log.debug("Registering GlobalServerInterceptor: {} ({})", name, interceptor);
    registry.addServerInterceptors(interceptor);
  }
}

代码示例来源:origin: Putnami/putnami-web-toolkit

@PostConstruct
public void afterPropertySet() {
  for (String beanName : this.applicationContext.getBeanNamesForAnnotation(this.serviceAnnotation)) {
    this.scanBean(this.applicationContext.getBean(beanName), beanName);
  }
}

代码示例来源: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: aatarasoff/spring-thrift-starter

@Override
@SneakyThrows({NoSuchMethodException.class, ClassNotFoundException.class, InstantiationException.class, IllegalAccessException.class})
protected void register(String description, ServletContext servletContext) {
  for (String beanName : applicationContext.getBeanNamesForAnnotation(ThriftController.class)) {
    ThriftController annotation = applicationContext.findAnnotationOnBean(beanName, ThriftController.class);
    register(servletContext, annotation.value(), protocolFactory.getClass(), applicationContext.getBean(beanName));
  }
}

代码示例来源:origin: org.bekit/flow

@PostConstruct
public void init() {
  String[] beanNames = applicationContext.getBeanNamesForAnnotation(Flow.class);
  for (String beanName : beanNames) {
    // 解析流程
    FlowExecutor flowExecutor = FlowParser.parseFlow(applicationContext.getBean(beanName), processorsHolder, flowTxsHolder, eventBusesHolder);
    if (flowExecutorMap.containsKey(flowExecutor.getFlowName())) {
      throw new RuntimeException("存在重名的流程" + flowExecutor.getFlowName());
    }
    // 将执行器放入持有器中
    flowExecutorMap.put(flowExecutor.getFlowName(), flowExecutor);
  }
}

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

private void scanForSections() {
  logger.debug("Scanning for side bar sections");
  String[] beanNames = applicationContext.getBeanNamesForAnnotation(MenuSection.class);
  for (String beanName : beanNames) {
    logger.debug("Bean [{}] declares a side bar section", beanName);
    addSectionDescriptors(applicationContext.findAnnotationOnBean(beanName, MenuSection.class));
  }
  beanNames = applicationContext.getBeanNamesForAnnotation(MenuSections.class);
  for (String beanName : beanNames) {
    logger.debug("Bean [{}] declares multiple side bar sections", beanName);
    addSectionDescriptors(applicationContext.findAnnotationOnBean(beanName, MenuSections.class).value());
  }
}

代码示例来源:origin: org.bekit/service

@PostConstruct
public void init() {
  String[] beanNames = applicationContext.getBeanNamesForAnnotation(Service.class);
  for (String beanName : beanNames) {
    // 解析服务
    ServiceExecutor serviceExecutor = ServiceParser.parseService(applicationContext.getBean(beanName), transactionManager);
    if (serviceExecutorMap.containsKey(serviceExecutor.getServiceName())) {
      throw new RuntimeException("存在重名的服务:" + serviceExecutor.getServiceName());
    }
    // 将执行器放入持有器中
    serviceExecutorMap.put(serviceExecutor.getServiceName(), serviceExecutor);
  }
}

代码示例来源:origin: org.bekit/flow

@PostConstruct
public void init() {
  String[] beanNames = applicationContext.getBeanNamesForAnnotation(Processor.class);
  for (String beanName : beanNames) {
    // 解析处理器
    ProcessorExecutor processorExecutor = ProcessorParser.parseProcessor(applicationContext.getBean(beanName));
    if (processorExecutorMap.containsKey(processorExecutor.getProcessorName())) {
      throw new RuntimeException("存在重名的处理器:" + processorExecutor.getProcessorName());
    }
    // 将执行器放入持有器中
    processorExecutorMap.put(processorExecutor.getProcessorName(), processorExecutor);
  }
}

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

private void scanForSections() {
  logger.debug("Scanning for side bar sections");
  String[] beanNames = applicationContext.getBeanNamesForAnnotation(SideBarSection.class);
  for (String beanName : beanNames) {
    logger.debug("Bean [{}] declares a side bar section", beanName);
    addSectionDescriptors(applicationContext.findAnnotationOnBean(beanName, SideBarSection.class));
  }
  beanNames = applicationContext.getBeanNamesForAnnotation(SideBarSections.class);
  for (String beanName : beanNames) {
    logger.debug("Bean [{}] declares multiple side bar sections", beanName);
    addSectionDescriptors(applicationContext.findAnnotationOnBean(beanName, SideBarSections.class).value());
  }
}

代码示例来源:origin: lord-of-code/loc-framework

private void init() {
 LocElasticJobProperties elasticJobProperties = resolverJobProperties();
 String[] jobs = this.applicationContext.getBeanNamesForAnnotation(LocElasticJob.class);
 ZookeeperRegistryCenter registryCenter = registerCenter(elasticJobProperties);
 JobEventConfiguration jobEventConfiguration = Optional
   .ofNullable(Strings.emptyToNull(elasticJobProperties.getDataSource())).map(
     s -> new JobEventRdbConfiguration(
       applicationContext.getBean(s + "Ds", DataSource.class))
   ).orElse(null);
 createBean(registryCenter, jobEventConfiguration, jobs);
}

相关文章