<context:component-scan base-package="com.a" /> 与<context:annotation-config />
2017-11-26 00:33
615 查看
在spring-servlet.xml或者applicationContext.xml中声明<context:component-scanpackage="com.a"/>可以隐式地向该上下文注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor ,从而使得该上下文中的bean都可以使用@Resource、@autowired等注解引用其他的bean(当然一并引入还有其他功能的注解,这里就不一一列举了),这个功能相当于声明<context:annotation-config/>。而<context:component-scan package="com.a"/>中的package="com.a",表示可以扫描com.a包下的类,将添加了@component等注解的类注册为上下文中的一个bean,如果写<context:component-scan
package="com.b"/>,则只启用@Resource等注解,但是不会通过@component注解来注册com.a包中的bean。例如
package com.a;
@Component
public class A{
@Resource
com.b.User user;
}
如果写 <context:component-scan
package="com.b" />,那么User类的bean可以注入(假如存在这个bean),但是@Component注解不会起作用,因此A不会注册为一个bean。
但<context:component-scan
package="com.a" />要求必须指定包名,如果仅仅是希望引入@resource @autowired等注解功能,使用
<context:annotation-config
/>即可。
package="com.b"/>,则只启用@Resource等注解,但是不会通过@component注解来注册com.a包中的bean。例如
package com.a;
@Component
public class A{
@Resource
com.b.User user;
}
如果写 <context:component-scan
package="com.b" />,那么User类的bean可以注入(假如存在这个bean),但是@Component注解不会起作用,因此A不会注册为一个bean。
但<context:component-scan
package="com.a" />要求必须指定包名,如果仅仅是希望引入@resource @autowired等注解功能,使用
<context:annotation-config
/>即可。
相关文章推荐
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.x
- Spring配置文件详解一:<:<context:annotation-config/>与<context:component-scan base-package="com.xx
- Spring配置文件详解一:<context:annotation-config/>与<context:component-scan base-package="com.xx
- 合并注解配置 <context:component-scan base-package="com.**.controller" annotation-config="true"/>
- Spring:<context:annotation-config/>与<context:component-scan base-package="com.xx />
- spring 中<context:annotation-config />和<context:component-scan base-package="com.*" >
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
- Spring <context:component-scan base-package=""/> 与 <context:annotation-config/>的区别
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotati...
- <context:component-scan base-package="">与 <context:annotation-config/>的区别
- Spring_<context:component-scan base-package="">和<context:annotation-config>学习
- spring mvc <mvc:annotation-driven/> 和<context:component-scan base-package=""/>解释
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <context:component-scan base-package=""> 与 <context:annotation-config 区别
- <mvc:annotation-driven/>、<context:component-scan base-package=""/>、<context:annotation-config/>的用法
- <context:component-scan base-package=""> <context:annotation-config>
- Spring <context:component-scan base-package="">和<context:annotation-config>
- spring中的<context:component-scan base-package="" />的作用
- <context:component-scan base-package="" />