context:component-scan base-package="" 与context:annotation-config

在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 />即可。

总之<context:component-scan package="com.a" />包含了

<context:annotation-config />的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值