Spring官方扩展的BeanPostProcessor

终于空了一会稍微看了下Spring的实现

发现Spring的几个annotation,@Autowired,@Required扩展的特别有意思,记录一下

首先要开启spring的Annotation的识别需要在xmlns中添加

xmlns:context="http://www.springframework.org/schema/context"

 然后指定下该schema的位置

http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd

 然后在beans中增加配置

<context:annotation-config/>

点开anotation-config可以看到这一串注释

Activates various annotations to be detected in bean classes: Spring's @Required and
	@Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available),
	JAX-WS's @WebServiceRef (if available), EJB3's @EJB (if available), and JPA's
	@PersistenceContext and @PersistenceUnit (if available). Alternatively, you may
	choose to activate the individual BeanPostProcessors for those annotations.

	Note: This tag does not activate processing of Spring's @Transactional or EJB3's
	@TransactionAttribute annotation. Consider the use of the <tx:annotation-driven>
	tag for that purpose.

 也就是说增加注释后,spring不仅可以识别spring自身@Required,@Autowired,也可以识别JSR 250中的@PostConstruct,@PreDestroy,@Resource(spring会先尝试load这些annotation的class,如果没有引入jsr-250的jar则作罢)同理JAX-WS的@WebServiceRef, EJB3的@EJB和JPA的@PersistenceContext和PersistenceUnit等。

不过对于事务,就不是这个配置管辖的范围了,请用<tx:annotation-driven>

 

那么Spring在读到这行后具体会增加哪些BeanPostProcessor呢,观察下来有4个

1.CommonAnnotationBeanPostProcessor

2.RequiredAnnotationBeanPostProcessor

3.AutowiredAnnotationBeanPostProcessor

4.ConfigurationClassPostProcessor

其中1-3都是Spring2.5中增加的功能,4是Spring 3.0中新增的

我们逐个来看下

1.CommonAnnotationBeanPostProcessor

这个是处理JSR 250中的那些和DI相关的annotation的

2.RequiredAnnotataionBeanPost

处理@Required注解

3.AutowiredAnnotationBeanPostProcessor

处理@Autowired注解,还有javax.inject.Inject,及value

4.ConfigurationClassPostProcessor

这个是Spring3.0新加入的,用来处理非xml形式的,java方式的依赖

 

看到这里可能有点蛋蛋的忧伤,怎么Autowired出现了他的好基友@Qualifier没出现呢?

其实@Qualifier的注解如下

This annotation may be used on a field or parameter as a qualifier for

 candidate beans when autowiring. It may also be used to annotate other

 custom annotations that can then in turn be used as qualifiers.

他就没说他是@Autowired的啊,在beans中配置default-autowire="",他也能生效的。

 

至于Qualifier的的实现,参见QualifierAnnotationAutowireCandidateResolver

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值