对springbean的深入理解,源码调试分析

如何映射controller?
如何映射静态页面?

直接定位到注册bean工厂

在这里插入图片描述

静态类方法注入
对springbean的深入理解,源码调试分析_第1张图片

且看beanFactory

对springbean的深入理解,源码调试分析_第2张图片
bean工厂获取bean的后置处理器
对springbean的深入理解,源码调试分析_第3张图片
其实这几个都在beanFactory中
对springbean的深入理解,源码调试分析_第4张图片
bean的后置处理器
对springbean的深入理解,源码调试分析_第5张图片
spring做了一个先删除 后添加的操作
对springbean的深入理解,源码调试分析_第6张图片
对springbean的深入理解,源码调试分析_第7张图片
对springbean的深入理解,源码调试分析_第8张图片



public static void registerBeanPostProcessors(
      ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {


   String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);


   // Register BeanPostProcessorChecker that logs an info message when
   // a bean is created during BeanPostProcessor instantiation, i.e. when
   // a bean is not eligible for getting processed by all BeanPostProcessors.
   int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
   beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));


   // Separate between BeanPostProcessors that implement PriorityOrdered,
   // Ordered, and the rest.
   List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
   List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
   List<String> orderedPostProcessorNames = new ArrayList<String>();
   List<String> nonOrderedPostProcessorNames = new ArrayList<String>();


   for (String ppName : postProcessorNames) {
      if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
         BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
         priorityOrderedPostProcessors.add(pp);
         if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
         }
      }
      else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
         orderedPostProcessorNames.add(ppName);
      }
      else {
         nonOrderedPostProcessorNames.add(ppName);
      }
   }


   // First, register the BeanPostProcessors that implement PriorityOrdered.
   sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
   registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);


   // Next, register the BeanPostProcessors that implement Ordered.
   List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
   for (String ppName : orderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      orderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   sortPostProcessors(beanFactory, orderedPostProcessors);
   registerBeanPostProcessors(beanFactory, orderedPostProcessors);


   // Now, register all regular BeanPostProcessors.   【开始注入bean】
   List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
   for (String ppName : nonOrderedPostProcessorNames) {
      BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
      nonOrderedPostProcessors.add(pp);
      if (pp instanceof MergedBeanDefinitionPostProcessor) {
         internalPostProcessors.add(pp);
      }
   }
   registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);


   // Finally, re-register all internal BeanPostProcessors.
   sortPostProcessors(beanFactory, internalPostProcessors);
   registerBeanPostProcessors(beanFactory, internalPostProcessors);


   // Re-register post-processor for detecting inner beans as ApplicationListeners,
   // moving it to the end of the processor chain (for picking up proxies etc).
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}


讨论一下,controller层中的方法映射是如何被扫码发现且打印出来的?

比如这样

10:19:33.108 [RMI TCP Connection(3)-127.0.0.1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/article/create],methods=[GET]}" onto public java.lang.String com.wangzhixuan.controller.ArticleController.create()
10:19:35.036 [RMI TCP Connection(3)-127.0.0.1] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/article/save],methods=[POST]}" onto public com.wangzhixuan.commons.result.Result com.wangzhixuan.controller.ArticleController.save(java.lang.String)

调试如下:
对springbean的深入理解,源码调试分析_第9张图片
对springbean的深入理解,源码调试分析_第10张图片

如何区分不同的bean ?

if (beanType != null && isHandler(beanType)) {
   detectHandlerMethods(beanName);
}

对springbean的深入理解,源码调试分析_第11张图片
其实就是判断某个类上是否有Controller或者 RequestMapping注解 卧槽
对springbean的深入理解,源码调试分析_第12张图片

然后再去查询增强控制层接口的类

对springbean的深入理解,源码调试分析_第13张图片
源码如下:


private void initControllerAdviceCache() {
   if (getApplicationContext() == null) {
      return;
   }
   if (logger.isInfoEnabled()) {
      logger.info("Looking for @ControllerAdvice: " + getApplicationContext());
   }


   List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
   AnnotationAwareOrderComparator.sort(beans);


   List<Object> requestResponseBodyAdviceBeans = new ArrayList<Object>();


   for (ControllerAdviceBean bean : beans) {
      Set<Method> attrMethods = MethodIntrospector.selectMethods(bean.getBeanType(), MODEL_ATTRIBUTE_METHODS);
      if (!attrMethods.isEmpty()) {
         this.modelAttributeAdviceCache.put(bean, attrMethods);
         if (logger.isInfoEnabled()) {
            logger.info("Detected @ModelAttribute methods in " + bean);
         }
      }
      Set<Method> binderMethods = MethodIntrospector.selectMethods(bean.getBeanType(), INIT_BINDER_METHODS);
      if (!binderMethods.isEmpty()) {
         this.initBinderAdviceCache.put(bean, binderMethods);
         if (logger.isInfoEnabled()) {
            logger.info("Detected @InitBinder methods in " + bean);
         }
      }
      if (RequestBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
         requestResponseBodyAdviceBeans.add(bean);
         if (logger.isInfoEnabled()) {
            logger.info("Detected RequestBodyAdvice bean in " + bean);
         }
      }
      if (ResponseBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
         requestResponseBodyAdviceBeans.add(bean);
         if (logger.isInfoEnabled()) {
            logger.info("Detected ResponseBodyAdvice bean in " + bean);
         }
      }
   }


   if (!requestResponseBodyAdviceBeans.isEmpty()) {
      this.requestResponseBodyAdvice.addAll(0, requestResponseBodyAdviceBeans);
   }
}

全局异常处理
对springbean的深入理解,源码调试分析_第14张图片
配置静态路径
对springbean的深入理解,源码调试分析_第15张图片

你可能感兴趣的:(spring,springfactory,springbean,spring源码,beanFactory,映射)