mysterin / question_and_answer

1 stars 0 forks source link

<context:annotation-config>, <context:component-scan>, <mvc:annotation-driven> 区别 #110

Closed mysterin closed 5 years ago

mysterin commented 5 years ago

  1. @Autowired 需要 AutowiredAnnotationBeanPostProcessor 的支持;
  2. @Resource, @PostConstruct, @PreDestroy 需要 CommonAnnotationBeanPostProcessor 的支持;
  3. @PersistenceContext 需要 PersistenceAnnotationBeanPostProcessor 的支持;
  4. @Required 需要 RequiredAnnotationBeanPostProcessor 的支持; 所以, <context:annotation-config /> 就是向容器隐式注册上面 4 个 BeanPostProcessor.

    <context:component-scan /> 包含了 <context:annotation-config /> 功能, 同时还支持 @Component, @Service, @Repository, @Controller 注解, 通过扫描对应包向容器注册对应 bean.

    相当于注册了 DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter 两个 bean, 即解决了@Controller 注解的使用前提配置.