---
title: "关于<context:annotation-config/><context:component-scan/>的作用"
author: "hou_jian_yong"
date: "2013-01-12T15:41:06Z"
tags: []
categories: ["开源软件"]
slug: "context-annotation-config-context-component-scan"
draft: false
lastmod: "2013-01-12T15:41:06Z"
id: 1768704
url: "https://espace.iteye.com/blog/1768704"
---

&lt;context:annotationconfig/&gt; 隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。

在配置文件中使用&lt;context:annotationconfig/&gt;之前，必须在 &lt;beans&gt; 元素中声明 context 命名空间&lt;context:component-scan/&gt;。

&lt;context:component-scan/&gt; 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能，同时还启用了注释驱动自动注入的功能（即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor），因此当使用 &lt;context:component-scan/&gt; 后，除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor两个Processor的功能（例如JPA等）否则就可以将 &lt;context:annotation-config/&gt; 移除了。 

 

原文地址：http://hi.baidu.com/coolcooldool/item/a5b3dccf5f47a017b77a2403