对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习;

当我们使用@Autowired、@Required等这些注解时,就要在spring容器里声明这些注解对应的java类的bean,那么需要在xml中进行配置;

如声明@Autowired:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>

声明@Required:

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即:

<context:annotation- config/>

隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。

我们做配置时一般也要加上扫描包路径选项:

<context:component-scan base-package="包全路径名称"/>

该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。

当然这里要注意的是配置<context:component-scan/>要加上相应的约束

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context/spring-context-3.0.xsd

  

最新文章

  1. Nginx学习笔记--001-Nginx快速搭建
  2. Egret 集成第三方库 记录
  3. python将图片转换为Framebuffer裸数据格式(终端显示图片)
  4. Linux学习笔记(5)-hello world
  5. 拿什么来拯救你,我的table
  6. 16进制字符串转数字(C/C++,VB/VB.net,C#)
  7. Java并发——线程池Executor框架
  8. nice Validator参考
  9. hadoop-ha组态
  10. Java Reflection 反射基础
  11. C++学习笔记第二天:几个知识点
  12. 重新学习Servlet二
  13. 我的Java自学之路
  14. JS 对数组的操作集锦(基础)
  15. python dataframe astype 字段类型转换
  16. SpringMVC系列(七)视图解析器和视图
  17. Android-Thread线程的状态
  18. 杭电 1800 Flying to the Mars(贪心)
  19. 1.2.1 工作流管理系统参考模型 (zhuan)
  20. js复制文字

热门文章

  1. 教你写Linux设备驱动程序:一个简短的教程
  2. C# 根据生日获取年龄
  3. java三角形和菱形的打印
  4. win10 安装git
  5. Swift:闭包(Closures)
  6. 【转】Ubuntu下出现Mysql error(2002)的解决方法
  7. Lazarus安装使用
  8. 文件系统之-JAVA Sftp远程操作:
  9. css控制打印时只显示指定区域
  10. 新建 .NET Core 控制台项目 C# 数组深拷贝