@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")); }
@Override public void afterPropertiesSet() { Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class); if (Objects.nonNull(beans)) { beans.forEach(this::registerContainer); } }
/** * 功能描述: 根据枚举类型获取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); }
/** * @return the {@link List} of REST (JAX-RS) {@link Provider}s. */ protected List<Object> findRestProviders() { return new ArrayList<>(this.applicationContext.getBeansWithAnnotation(Provider.class).values()); }
@Override public void afterSingletonsInstantiated() { Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class); if (Objects.nonNull(beans)) { beans.forEach(this::registerContainer); } }
@Override public void afterPropertiesSet() { Map<String, Object> beans = this.applicationContext.getBeansWithAnnotation(RocketMQMessageListener.class); if (Objects.nonNull(beans)) { beans.forEach(this::registerContainer); } }
@Override public Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException { lock.readLock().lock(); try { return configurableApplicationContext.getBeansWithAnnotation(annotationType); } finally { lock.readLock().unlock(); } }
/** * 根据枚举类型获取Spring注册的Bean * * @param annotationType 枚举 * @return */ public Map<String, Object> getBeanWithAnnotation(Class<? extends Annotation> annotationType) { Assert.notNull(annotationType); return cfgContext.getBeansWithAnnotation(annotationType); }
/** * 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()); } }
/** * 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()); } }
@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; }
@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; }
@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; }
/** * 遍历容器的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); } }
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; }
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; }
@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"); }
@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; }
@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); }
@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")); }