zoukankan      html  css  js  c++  java
  • annotation-config和component-scan

      以前学到<context:annotation-config></context:annotation-config>和<context:component-scan base-package="" ></context:component-scan>和两个配置的时候,只知道,annotation-config是用来开启xml配置中的bean对注解的支持,而component-scan是用来开启注解扫描,扫描指定包下面带注解的类。

      今天看源码有了点新的收获:

        annotation-config这个标签,实际上是注册各种BeanPostProcesser到BeanFactory,比如:AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这些,看名字就知道是干啥的了,因此xml配置的bean也就能通过注解注入了。

        context:component-scan标签,实际是将配置的类路径转换为文件路径,比如:base-package="com.zby",转换为classpath*:com/zby/**/*.class,然后把这些加载的字节码封装成Resource,通过ASM读取字节码信息,封装成ScannedGenericBeanDefinition。这样就可以注册到BeanFactory了。同时,component-scan标签包含了annotation-config的功能,也注册了一系列对注解支持的BeanPostProcesser,所以,就不要同时出现这两个标签了。

        看到这儿,也终于解决一个困惑,之前一直不知道asm在spring用来干嘛的,asm是用来扫描包,解析字节码的,cglib用来做代理增强。

  • 相关阅读:
    Python 文件操作
    Python 操作 sqlite
    Python中的random模块
    Linux系统下的/etc/nsswitch.conf文件
    Python 列表/元组/字典总结
    快斗之翼:python2的print和python3的print()
    田小计划:图解Python深拷贝和浅拷贝
    Python 自省指南
    Python运算符优先级
    tc: 模拟网络异常的工具
  • 原文地址:https://www.cnblogs.com/zby9527/p/8920633.html
Copyright © 2011-2022 走看看