Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.xx
2017-02-20 14:05
851 查看
1.<context:annotation-config/>
2.<context:component-scan base-package="com.xx" />
3. @Autowired
在基于主机方式配置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会自动完成声明。 |
2.<context:component-scan base-package="com.xx" />
<context:component-scan/>的作用是让Bean定义注解工作起来,也就是上述传统声明方式。 它的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 值得注意的是<context:component-scan/>不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。 |
3. @Autowired
@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。注解之后就不需要set/get方法了。 |
相关文章推荐
- Spring配置文件详解一:<:<context:annotation-config/>与<context:component-scan base-package="com.xx
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.x
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotati...
- Spring:<context:annotation-config/>与<context:component-scan base-package="com.xx />
- 关于使用注解的Spring配置文件的配置<context:annotation-config/>&&<context:component-scan base-package=”XX.XX”/>
- 合并注解配置 <context:component-scan base-package="com.**.controller" annotation-config="true"/>
- spring 中<context:annotation-config />和<context:component-scan base-package="com.*" >
- Spring的配置文件applicationContext.xml中的 <context:component-scan base-package="com.orm" />
- Spring <context:component-scan base-package="">和<context:annotation-config>
- Spring_<context:component-scan base-package="">和<context:annotation-config>学习
- Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
- <context:component-scan base-package="com.a" /> 与<context:annotation-config />
- spring mvc <mvc:annotation-driven/> 和<context:component-scan base-package=""/>解释
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- 【小平工作日志】Spring注解注入配置:<context:component-scan base-package="**.**"/>详解!
- Spring:<context:component-scan base-package=com.xx.yy/ >注解
- ssh之<context:component-scan base-package="com.xx" />
- [spring]Spring <context:component-scan base-package="">和<context:annotation-config>
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法