Spring框架中Spring配置文件中<context:annotation-config/>标签说明

<context:annotation-config/>此标签的重要作用就是:

  省去系统繁琐的注解标签,加上一个此标签,就可以在此项目程序添加“注解”的功能,使系统识别相应的注解功能!!

详解:

<context:annotation-config/>标签的作用是隐式地向 Spring 容器注册AutowiredAnnotationBeanPostProcessor、

CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这4个BeanPostProcessor。

注册这4个 BeanPostProcessor的作用,就是为了让系统能识别相应的注解。

比如系统繁琐的注解功能:

1:如果使用@Autowired注解:

必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下

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

2:如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

3:想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

4:想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

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

总结:这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供的简化配置方式,自动帮你完成声明。-----即用<context:annotation-config/>标签即可;

附加:使用注解一般还要用到扫描包路径的选项:<context:component-scan base-package=”XX.XX”/>

本人目前处于学习阶段,各位大神多多提宝贵的建议!
原文地址:https://www.cnblogs.com/FanSunny/p/4720824.html