<context:component-scan>与 <context:annotation-config>
来源:互联网 发布:易语言登陆器源码 编辑:程序博客网 时间:2024/05/22 14:10
<context:annotation-config>
它的作用是隐式的向Spring容器注册
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
这4个BeanPostProcessor.注册这4个bean处理器主要的作用是为了你的系统能够识别相应的注解。
例如:
如果想使用@Autowired注解,需要在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。
传统的声明方式如下:
<bean class= "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
如果想使用@PersistenceContext注解,需要在Spring容器中声明PersistenceAnnotationBeanPostProcessor Bean。
传统的声明如下:
<bean class= "org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
如果想使用@Required注解,需要在Spring容器中声明RequiredAnnotationBeanPostProcessor Bean。
传统声明方式如下:
<bean class= "org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
如果想使用@Resource、@ PostConstruct、@ PreDestroy等注解就要声明CommonAnnotationBeanPostProcessor。
传统声明方式如下:
<bean class= "org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor" />
所以,如果按照传统声明一条一条去声明注解Bean,就会显得十分繁琐。因此如果在Spring的配置文件中事先加上`<context:annotation-config/>`这样一条配置的话,那么所有注解的传统声明就可以被忽略,即不用再写传统的声明,Spring会自动完成声明。
<context:component-scan base-package="com.xx" />
`<context:component-scan/>`的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
两者区别: 值得注意的是<context:component-scan/>
不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/>
后,就可以将 <context:annotation-config/>
移除了。
0 0
- <context:annotation-config /> 与<context:component-scan />
- <context:component-scan>与 <context:annotation-config>
- spring <context:component-scan/>与<context:annotation-config/>
- Spring IOC <context:annotation-config>与<context:component-scan/>
- <context:annotation-config> & <context:component-scan>
- <context:annotation-config>和<context:component-scan>
- <context:component-scan>,<context:annotation-config/>,<mvc:annotation-scan/>
- <mvc:annotation-driven/>,<context:annotation-config/>,<context:component-scan/>详解
- <mvc:annotation-driven/> <context:annotation-config/> <context:component-scan>
- Spring <context:annotation-config/> <context:component-scan/>与<mvc:annotation-driven />
- <context:component-scan base-package=""> <context:annotation-config>
- Spring <context:annotation-config/> 、<context:component-scan>解说
- Spring <context:annotation-config/> 和 <context:component-scan>
- Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
- context:annotation-config配置和context:component-scan配置
- <context:annotation-config/>和context:component-scan的区别
- <context:annotation-config> vs <context:component-scan> 的区别
- Difference between <context:annotation-config> vs <context:component-scan>
- 制作工具类及说明文档
- 机器学习实战之Logistic回归
- 我的原创博客(个人用)
- [2017.04]《万万没想到:用理工科思维理解世界》
- JAVA接口
- <context:component-scan>与 <context:annotation-config>
- 优雅的开发Swift和Objective C混编的Framework
- java中匹配中文插入字符串
- 有赞API接口 SDK 的开发经历
- Android 环信 自定义聊天气泡
- swift 创建UIButton
- vector的使用
- scope
- python元组的创建、赋值、更新、删除等操作实例源码