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

x33g5p2x  于2022-01-18 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(185)

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

ConfigurableApplicationContext.getBeansWithAnnotation介绍

暂无

代码示例

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

@Test
public void isProxy() throws Exception {
  TransactionalTestBean bean = getTestBean();
  assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
  Map<String, Object> services = this.context.getBeansWithAnnotation(Service.class);
  assertTrue("Stereotype annotation not visible", services.containsKey("testBean"));
}

代码示例来源:origin: javahongxi/whatsmars

@Override
public void afterPropertiesSet() {
  Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class);
  if (Objects.nonNull(beans)) {
    beans.forEach(this::registerContainer);
  }
}

代码示例来源:origin: chenjy16/gts

/**
 * 功能描述: 根据枚举类型获取Spring注册的Bean
 * @author: chenjy
 * @date: 2017年9月18日 下午2:28:28 
 * @param annotationType
 * @return
 */
public Map<String, Object> getBeanWithAnnotation(Class<? extends Annotation> annotationType) {
  return cfgContext.getBeansWithAnnotation(annotationType);
}

代码示例来源:origin: oasp/oasp4j

/**
 * @return the {@link List} of REST (JAX-RS) {@link Provider}s.
 */
protected List<Object> findRestProviders() {
 return new ArrayList<>(this.applicationContext.getBeansWithAnnotation(Provider.class).values());
}

代码示例来源:origin: org.apache.rocketmq/rocketmq-spring-boot

@Override
public void afterSingletonsInstantiated() {
  Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class);
  if (Objects.nonNull(beans)) {
    beans.forEach(this::registerContainer);
  }
}

代码示例来源:origin: QianmiOpen/spring-boot-starter-rocketmq

@Override
public void afterPropertiesSet() {
  Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class);
  if (Objects.nonNull(beans)) {
    beans.forEach(this::registerContainer);
  }
}

代码示例来源:origin: NationalSecurityAgency/datawave

@Override
public Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException {
  lock.readLock().lock();
  try {
    return configurableApplicationContext.getBeansWithAnnotation(annotationType);
  } finally {
    lock.readLock().unlock();
  }
}

代码示例来源:origin: keets2012/Lottor

/**
 * 根据枚举类型获取Spring注册的Bean
 *
 * @param annotationType 枚举
 * @return
 */
public Map<String, Object> getBeanWithAnnotation(Class<? extends Annotation> annotationType) {
  Assert.notNull(annotationType);
  return cfgContext.getBeansWithAnnotation(annotationType);
}

代码示例来源:origin: nhuray/dropwizard-spring

/**
 * Register objects annotated with {@link Provider} in Dropwizard {@link Environment} from Spring application context.
 *
 * @param environment the Dropwizard environment
 * @param context     the Spring application context
 */
private void registerProviders(Environment environment, ConfigurableApplicationContext context) {
  final Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(Provider.class);
  for (String beanName : beansWithAnnotation.keySet()) {
    // Add injectableProvider to Dropwizard environment
    Object provider = beansWithAnnotation.get(beanName);
    environment.jersey().register(provider);
    LOG.info("Registering provider : " + provider.getClass().getName());
  }
}

代码示例来源:origin: nhuray/dropwizard-spring

/**
 * Register resources annotated with {@link Path} in Dropwizard {@link Environment} from Spring application context.
 *
 * @param environment the Dropwizard environment
 * @param context     the Spring application context
 */
private void registerResources(Environment environment, ConfigurableApplicationContext context) {
  final Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(Path.class);
  for (String beanName : beansWithAnnotation.keySet()) {
    // Add injectableProvider to Dropwizard environment
    Object resource = beansWithAnnotation.get(beanName);
    environment.jersey().register(resource);
    LOG.info("Registering resource : " + resource.getClass().getName());
  }
}

代码示例来源:origin: maihaoche/rocketmq-spring-boot-starter

@PostConstruct
public void init() throws Exception {
  Map<String, Object> beans = applicationContext.getBeansWithAnnotation(MQConsumer.class);
  if(!CollectionUtils.isEmpty(beans) && mqProperties.getTraceEnabled()) {
    initAsyncAppender();
  }
  validConsumerMap = new HashMap<>();
  for (Map.Entry<String, Object> entry : beans.entrySet()) {
    publishConsumer(entry.getKey(), entry.getValue());
  }
  // 清空map,等待回收
  validConsumerMap = null;
}

代码示例来源:origin: line/line-bot-sdk-java

@VisibleForTesting
void refresh() {
  final Map<String, Object> handlerBeanMap =
      applicationContext.getBeansWithAnnotation(LineMessageHandler.class);
  final List<HandlerMethod> collect = handlerBeanMap
      .values().stream()
      .flatMap((Object bean) -> {
        final Method[] uniqueDeclaredMethods =
            ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());
        return Arrays.stream(uniqueDeclaredMethods)
               .map(method -> getMethodHandlerMethodFunction(bean, method))
               .filter(Objects::nonNull);
      })
      .sorted(HANDLER_METHOD_PRIORITY_COMPARATOR)
      .collect(Collectors.toList());
  log.info("Registered LINE Messaging API event handler: count = {}", collect.size());
  collect.forEach(item -> log.info("Mapped \"{}\" onto {}",
                   item.getSupportType(), item.getHandler().toGenericString()));
  eventConsumerList = collect;
}

代码示例来源:origin: com.linecorp.bot/line-bot-spring-boot

@VisibleForTesting
void refresh() {
  final Map<String, Object> handlerBeanMap =
      applicationContext.getBeansWithAnnotation(LineMessageHandler.class);
  final List<HandlerMethod> collect = handlerBeanMap
      .values().stream()
      .flatMap((Object bean) -> {
        final Method[] uniqueDeclaredMethods =
            ReflectionUtils.getUniqueDeclaredMethods(bean.getClass());
        return Arrays.stream(uniqueDeclaredMethods)
               .map(method -> getMethodHandlerMethodFunction(bean, method))
               .filter(Objects::nonNull);
      })
      .sorted(HANDLER_METHOD_PRIORITY_COMPARATOR)
      .collect(Collectors.toList());
  log.info("Registered LINE Messaging API event handler: count = {}", collect.size());
  collect.forEach(item -> log.info("Mapped \"{}\" onto {}",
                   item.getSupportType(), item.getHandler().toGenericString()));
  eventConsumerList = collect;
}

代码示例来源:origin: com.github.rebue.wheel/wheel-core

/**
 * 遍历容器的bean,并匹配有指定注解类型的bean
 */
public static void foreachBeansWithAnnotation(Class<? extends Annotation> annotationType, final BeanMatcher beanMatcher) throws Exception {
  Map<String, Object> beans = _applicationContext.getBeansWithAnnotation(annotationType);
  // 将Spring容器中的服务bean转成Key为接口全名的Map
  Object tempBean;
  for (Object bean : beans.values()) {
    // 如果bean被Spring的AOP包装
    if (AopUtils.isAopProxy(bean))
      tempBean = SpringAopHelper.getTarget(bean);
    else
      tempBean = bean;
    beanMatcher.matched(tempBean);
  }
}

代码示例来源:origin: leangen/graphql-spqr-spring-boot-starter

private Map<String, SpqrBean> findGraphQLApiComponents() {
  final Map<String, Object> operationSourcesBeans = context.getBeansWithAnnotation(GraphQLApi.class);
  Map<String, SpqrBean> result = new HashMap<>();
  for (String beanName : operationSourcesBeans.keySet()) {
    Class<?> operationSourceBeanClass = operationSourcesBeans.get(beanName).getClass();
    result.put(beanName, new SpqrBean(context, beanName, GenericTypeReflector.annotate(ClassUtils.getUserClass(operationSourceBeanClass))));
    if (operationSourceBeanClass.isAnnotationPresent(WithResolverBuilder.class)) {
      WithResolverBuilder withResolverBuilder = operationSourceBeanClass.getAnnotation(WithResolverBuilder.class);
      result.get(beanName).resolverBuilders.add(new ResolverBuilderBeanIdentity(withResolverBuilder.value(), withResolverBuilder.qualifierValue(), withResolverBuilder.qualifierType()));
    } else if (operationSourceBeanClass.isAnnotationPresent(WithResolverBuilders.class)) {
      for (WithResolverBuilder withResolverBuilder : operationSourceBeanClass.getAnnotation(WithResolverBuilders.class).value()) {
        result.get(beanName).resolverBuilders.add(new ResolverBuilderBeanIdentity(withResolverBuilder.value(), withResolverBuilder.qualifierValue(), withResolverBuilder.qualifierType()));
      }
    }
  }
  return result;
}

代码示例来源:origin: io.leangen.graphql/graphql-spqr-spring-boot-autoconfigure

private Map<String, SpqrBean> findGraphQLApiComponents() {
  final Map<String, Object> operationSourcesBeans = context.getBeansWithAnnotation(GraphQLApi.class);
  Map<String, SpqrBean> result = new HashMap<>();
  for (String beanName : operationSourcesBeans.keySet()) {
    Class<?> operationSourceBeanClass = operationSourcesBeans.get(beanName).getClass();
    result.put(beanName, new SpqrBean(context, beanName, GenericTypeReflector.annotate(ClassUtils.getUserClass(operationSourceBeanClass))));
    if (operationSourceBeanClass.isAnnotationPresent(WithResolverBuilder.class)) {
      WithResolverBuilder withResolverBuilder = operationSourceBeanClass.getAnnotation(WithResolverBuilder.class);
      result.get(beanName).resolverBuilders.add(new ResolverBuilderBeanIdentity(withResolverBuilder.value(), withResolverBuilder.qualifierValue(), withResolverBuilder.qualifierType()));
    } else if (operationSourceBeanClass.isAnnotationPresent(WithResolverBuilders.class)) {
      for (WithResolverBuilder withResolverBuilder : operationSourceBeanClass.getAnnotation(WithResolverBuilders.class).value()) {
        result.get(beanName).resolverBuilders.add(new ResolverBuilderBeanIdentity(withResolverBuilder.value(), withResolverBuilder.qualifierValue(), withResolverBuilder.qualifierType()));
      }
    }
  }
  return result;
}

代码示例来源:origin: line/line-bot-sdk-java

@Test
public void testPrioritySelection() throws Exception {
  when(applicationContext.getBeansWithAnnotation(LineMessageHandler.class))
      .thenReturn(ImmutableMap.of("bean", new MessageHandler(),
                    "anothrer", new AnotherMessageHandler()));
  // Do
  target.refresh();
  // Verify
  assertThat(target.eventConsumerList).hasSize(3);
  // 1st
  assertThat(target.eventConsumerList.get(0).getHandler().getName())
      .isEqualTo("textMessageEventHandler");
  // 2nd
  assertThat(target.eventConsumerList.get(1).getHandler().getName())
      .isEqualTo("generalMessageHandler");
  // 3rd
  assertThat(target.eventConsumerList.get(2).getHandler().getName())
      .isEqualTo("defaultEventHandler");
}

代码示例来源:origin: maihaoche/rocketmq-spring-boot-starter

@Bean
public DefaultMQProducer exposeProducer() throws Exception {
  Map<String, Object> beans = applicationContext.getBeansWithAnnotation(MQProducer.class);
  //对于仅仅只存在消息消费者的项目,无需构建生产者
  if(CollectionUtils.isEmpty(beans)){
    return null;
  }
  if(producer == null) {
    Assert.notNull(mqProperties.getProducerGroup(), "producer group must be defined");
    Assert.notNull(mqProperties.getNameServerAddress(), "name server address must be defined");
    producer = new DefaultMQProducer(mqProperties.getProducerGroup());
    producer.setNamesrvAddr(mqProperties.getNameServerAddress());
    producer.setSendMsgTimeout(mqProperties.getSendMsgTimeout());
    producer.setSendMessageWithVIPChannel(mqProperties.getVipChannelEnabled());
    producer.start();
  }
  return producer;
}

代码示例来源:origin: line/line-bot-sdk-java

@Test
public void testRefreshForOneItem() throws Exception {
  when(applicationContext.getBeansWithAnnotation(LineMessageHandler.class))
      .thenReturn(singletonMap("bean", new MessageHandler()));
  // Do
  target.refresh();
  // Verify
  assertThat(target.eventConsumerList).hasSize(1);
  HandlerMethod handlerMethod = target.eventConsumerList.get(0);
  assertThat(handlerMethod.getSupportType())
      .isInstanceOf(Predicate.class);
  assertThat(handlerMethod.getHandler())
      .isInstanceOf(Method.class);
}

代码示例来源:origin: line/line-bot-sdk-java

@Test
public void dispatchAndReplyMessageTest() {
  final MessageEvent event = EventTestUtil.createTextMessage("text");
  when(applicationContext.getBeansWithAnnotation(LineMessageHandler.class))
      .thenReturn(singletonMap("bean", new ReplyHandler("Message from Handler method")));
  target.refresh();
  // Do
  target.dispatch(event);
  // Verify
  verify(replyByReturnValueConsumerFactory).createForEvent(event);
  verify(replyByReturnValueConsumer, times(1)).accept(new TextMessage("Message from Handler method"));
}

相关文章

微信公众号

最新文章

更多

ConfigurableApplicationContext类方法