Spring配置文件中 注解标签 详解
来源:互联网 发布:神经网络算法代码 编辑:程序博客网 时间:2024/06/05 18:29
<context:annotation-config/>
在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<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.xx" />
<context:component-scan/>
的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
spring的 component-scan标签 扫描多个包:
<context:component-scan base-package="x.y.z.service, x.y.z.controller" />
注意这里是,分隔两个包名,必须是 包名。
值得注意的是<context:component-scan/>
不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor)因此当使用 <context:component-scan/>
后,就可以将 <context:annotation-config/>
移除了。
@Autowired
可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。
注意:如果有多个配置文件,在最顶层的配置文件(启动类所在的配置文件)中加入<context:component-scan base-package="com.xx.xx" />
。(如controller层的配置文件中)
<mvc:annotation-driven />
它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
结论:在spring-servlet.xml中只需要扫描所有带@Controller注解的类,在applicationContext中可以扫描所有其他带有注解的类(也可以过滤掉带@Controller注解的类)。
- Spring配置文件中 注解标签 详解
- spring配置文件详解--注解方式
- spring配置文件中bean标签中init-method和destroy-method和用注解方式配置
- Spring注解标签详解@Autowired @Qualifier等
- Spring注解标签详解@Autowired @Qualifier等
- Spring mvc常用注解标签详解
- Spring注解标签详解@Autowired @Qualifier
- Spring注解标签详解@Autowired @Qualifier @Component
- Spring各种注解标签作用详解
- spring 中配置文件配置注解方法
- Spring中使用注解代替xml配置文件
- Spring中的注入注解Resource/Autowired以及配置文件中default-autowire参数详解
- Spring中@Component注解,@Controller注解详解
- Spring中@Component注解,@Controller注解详解
- spring配置文件中autowire详解
- Spring中注解标签的解释
- spring配置文件中ref 和idref标签
- log4j2.x配置文件中各标签详解
- DIV居中
- 戏说slub分配器
- OSB12C socket TRANSPORT配置
- 内边距
- Vijos P1304 回文数【回文+进制】
- Spring配置文件中 注解标签 详解
- js控制滚动条(简单插件手写)
- 使用sklearn 决策树,KNN,以及自定义KNN类来实现分类
- [设计模式]原型模式(Prototype)
- Mac下QT错误,Xcode配置解决办法
- SDL学习(001):走好第一步——VS2017中的基本设置
- 性能分析思路
- C++ 11 之学习总结
- 使用 u-boot 烧写内核——韦东山嵌入式Linux学习笔记10