在afterPropertiesSet方法中,我们看到定义了一个后处理器postProcessors,里面注册了相关的注解处理类。包含各种消息端点处理,除了上面写的ServiceActivator,还有过滤器,路由,转换器等各种不同的端点方法。
接着往向下看,既然实现了BeanPostProcessor,那必然要用到postProcessAfterInitialization方法实现,这里的流程大概就是遍历出包含有@ServiceActivator的bean方法,用来做后续处理。我们直接看重点的代码。
Object result = postProcessor.postProcess(bean, beanName, targetMethod, annotations);
三,postProcess在AbstractMethodAnnotationPostProcessor中有个共通方法postProcess用来生成对应的端点信息。具体代码:
@Override public Object postProcess(Object bean, String beanName, Method method, List<Annotation> annotations) { Object sourceHandler = null; if (beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { if (!this.beanFactory.containsBeanDefinition(resolveTargetBeanName(method))) { this.logger.debug("Skipping endpoint creation; perhaps due to some '@Conditional' annotation."); return null; } else { sourceHandler = resolveTargetBeanFromMethodWithBeanAnnotation(method); } } //生成对应的MessageHandler,用来执行对应的注解的方法 MessageHandler handler = createHandler(bean, method, annotations); if (!(handler instanceof ReactiveMessageHandlerAdapter)) { orderable(method, handler); producerOrRouter(annotations, handler); if (!handler.equals(sourceHandler)) { handler = registerHandlerBean(beanName, method, handler); } handler = annotated(method, handler); handler = adviceChain(beanName, annotations, handler); } //将MessageHandler实现连接到消息端点,生成对应的endpoint。 AbstractEndpoint endpoint = createEndpoint(handler, method, annotations); if (endpoint != null) { return endpoint; } else { return handler; } }这里面主要是两件事:
根据模板模式中不同的createHandler抽象方法实现,生成对应的MessageHandler。譬如说我们这边的ServiceActivatorAnnotationPostProcessor
将MessageHandler实现连接到消息端点,生成对应的endpoint。
1.createHandler @Override protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) { AbstractReplyProducingMessageHandler serviceActivator; if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) { ... else { serviceActivator = new ServiceActivatingHandler(bean, method); } String requiresReply = MessagingAnnotationUtils.resolveAttribute(annotations, "requiresReply", String.class); if (StringUtils.hasText(requiresReply)) { serviceActivator.setRequiresReply(resolveAttributeToBoolean(requiresReply)); } String isAsync = MessagingAnnotationUtils.resolveAttribute(annotations, "async", String.class); if (StringUtils.hasText(isAsync)) { serviceActivator.setAsync(resolveAttributeToBoolean(isAsync)); } //是否设置了输出通道 setOutputChannelIfPresent(annotations, serviceActivator); return serviceActivator; }createHandler的代码比较简单,就是根据注解中的几个属性还有对应的方法参数,生成ServiceActivatingHandler。追溯下去ServiceActivatingHandler中最后会生成一个委托对象MessagingMethodInvokerHelper用来以反射的方式来执行目标方法。
2.createEndpointcreateEndpoint字面上都能知道是生成消息端点,事实上也是,把生成的handler和对应的管道进行关联。具体看下代码体会:
protected AbstractEndpoint createEndpoint(MessageHandler handler, @SuppressWarnings("unused") Method method, List<Annotation> annotations) { AbstractEndpoint endpoint = null; //取得注解中inputChannelName String inputChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, getInputChannelAttribute(), String.class); if (StringUtils.hasText(inputChannelName)) { MessageChannel inputChannel; try { //从beanFactory中取得对应的通道bean inputChannel = this.channelResolver.resolveDestination(inputChannelName); } catch (DestinationResolutionException e) { //取不到,则自动注册一个类型为DirectChannel的inputChannel if (e.getCause() instanceof NoSuchBeanDefinitionException) { inputChannel = new DirectChannel(); this.beanFactory.registerSingleton(inputChannelName, inputChannel); inputChannel = (MessageChannel) this.beanFactory.initializeBean(inputChannel, inputChannelName); if (this.disposables != null) { this.disposables.add((DisposableBean) inputChannel); } } else { throw e; } } Assert.notNull(inputChannel, () -> "failed to resolve inputChannel '" + inputChannelName + "'"); //生成endpoint endpoint = doCreateEndpoint(handler, inputChannel, annotations); } return endpoint; }