<context:annotation-config/>
annotation-config将会在Spring容器注册AutowiredAnnotationBeanPostProcessor(该bean可以自动装配具有@Autowired、@Resource、@Inject等注解的属性),而不需要我们再使用<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>这种方式手动注入该bean了。除了上述的那个bean,还有CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这三个BeanPostProcessor。<context:compnent-scan/>
component-scan会对包进行扫描,实现注解驱动 Bean 定义。即将@Controller标识的类的bean注册到容器中,不过在@Component语义基础上细化后的@Repository,@Service和@Controller也同样可以。同时component-scan还启用了注解驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor)。因此当使用<context:component-scan/> 后,除非需要使用PersistenceAnnotationBeanPostProcessor和RequiredAnnotationBeanPostProcessor两个Processor的功能(例如 JPA 等),否则就可以不需要配置<context:annotation-config/>。
<context:component-scan base-package="org.zero.spring4.test" /> <mvc:annotation-driven/>
annotation-driven会向Spring容器中注册 DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter,解决@Controller注解的使用前提配置,即 HandlerMapping 能够知道谁来处理请求。

被折叠的 条评论
为什么被折叠?



