`
reverocean
  • 浏览: 192903 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Spring容器的扩展点

阅读更多
摘录自Spring文档的3.8节。

1.BeanPostProcessor
BeanPostProcessor接口有两个方法:
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

可以在容器中的每个bean实例化之前和之后加上特殊的处理。Spring提供的RequiredAnnotationBeanPostProcessor就是实现该接口以判断容器里的bean里所标注annotation的属性是否被注入正确的值。

在容器中你可以配置多个实现BeanPostProcessor的bean,使用order属性来确定其执行顺序。

2.BeanFactoryPostProcessor
BeanFactoryPostProcessor接口和BeanPostProcessor接口的运行点是一样的,唯一不一样的就是处理的内容不同。BeanPostProcessor处理的是bean的内容,该接口处理的是在spring配置中的metadata。
Spring提供的处理${表达式}的类PropertyPlaceholderConfigurer就是该接口的实现。
<context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
PropertyPlaceholderConfigurer不只是读取配置在location的properties文件里的属性,当在配置文件中找不到属性的时候,可以读取Java System的Properties,也可以从其属性properties里得到。可以通过设置该bean的systemPropertiesMode属性的值(always override, never override, and override only if the property is not found in the properties file specified)来设置设置对应表达式的值。


3.FactoryBean

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics