Spring在解析完配置文件后,会调用一些callback方法,使用Spring的开发者可以通过提供这些callback方法达到Spring Container的扩展.
1,通过实现BeanPostProcessor来完成对Bean的一些'定制',BeanPostProcessor定义了两个方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName).
postProcessBeforeInitialization会在Spring初始化bean之后,在Container调用申明的initialization方法(如afterPropertiesSet)之前被调用,postProcessAfterInitialization会在在Container调用申明的initialization方法(如afterPropertiesSet)之后被调用.如果需要有多个实现BeanPostProcessor的类,可以通过让其实现Ordered接口来控制这些类的callback被调用的顺序.
这种bean的post-processor一般用来检查bean是否实现了某个接口,或者把bean和某个proxy包装起来,Spring的AOP某些框架类就是实现了BeanPostProcessor.
例:
public class MyBeanPostProcessor implements BeanPostProcessor,
InvocationHandler {
private Object proxyobj;
public MyBeanPostProcessor() {
}
public MyBeanPostProcessor(Object obj) {
proxyobj = obj;
}
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessBeforeInitialization Bean '" + beanName
+ "' created : " + bean.toString());
if (bean instanceof Intf) {
Class cls = bean.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls
.getInterfaces(), new MyBeanPostProcessor(bean));
} else {
return bean;
}
}
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("postProcessAfterInitialization Bean '" + beanName
+ "' created : " + bean.toString());
return bean;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("proxy is " + proxy.getClass().getName());
System.out.println("before calling " + method);
if (args != null) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i] + "");
}
}
Object o = method.invoke(proxyobj, args);
System.out.println("after calling " + method);
return o;
}
}
public interface Intf {
public String testFunc();
}
public class IntfBean implements Intf {
private String strVal = "default value";
@Override
public String testFunc() {
// TODO Auto-generated method stub
return strVal;
}
}
<bean id="IntfBean" class="com.test.spring.extent.IntfBean" />
<bean class="com.test.spring.extent.MyBeanPostProcessor" />
调用代码:
Intf intfBean = (Intf) ctx.getBean("IntfBean");
System.out.println(intfBean.testFunc());
这样,MyBeanPostProcessor会把实现Intf接口的bean和一个proxy包装起来.
2,通过实现BeanFactoryPostProcessor接口来操作配置文件,对配置的元数据进行'特制'.Spring自身的PropertyPlaceholderConfigurer就实现了
这个接口,通过对XML配置文件中使用占位符,PropertyPlaceholderConfigurer从别的property文件中读取值进行替换.
例子:从ext.properties读取值替换${TEST.PROP1},${TEST.PROP2}.
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:conf/ext.properties" />
<property name="properties">
<value>TEST.PROP3=inside property</value>
</property>
</bean>
<bean id="PropReplaceBean" class="com.test.spring.extent.PropReplaceBean">
<property name="strVal" value="${TEST.PROP1}" />
<property name="intVal" value="${TEST.PROP2}" />
<property name="insideProp" value="${TEST.PROP3}" />
</bean>
通常比较有用的场景是数据库的url,用户名,密码的配置,还可以用来动态指定某个bean的类名,把XMl文件和property文件分开维护更容易.
<bean id="xxxBean" class="${com.xxx.class}"/>
自定义的BeanFactoryPostProcessor,通过实现Ordered接口可以改变被callback的顺序.callback方法中的ConfigurableListableBeanFactory beanFactory提供了配置文件的元数据.(*碰到一个困惑的问题是: PropertyPlaceholderConfigurer的order是 Integer.MAX_VALUE,却发现它比自己定义的order 为-1的 processor先执行.)
<bean class="com.test.spring.extent.MyBeanFactoryPostProcessor" />
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
Ordered {
private int order = -1;
public void setOrder(int order) {
this.order = order;
}
public int getOrder() {
return this.order;
}
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition beanDefinition = beanFactory
.getBeanDefinition("PropReplaceBean");
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
PropertyValue[] pvArray = pvs.getPropertyValues();
for (PropertyValue pv : pvArray) {
System.out.println(pv.getName() + " " + pv.getValue().getClass());
}
}
}
Spring检测到BeanPostProcessor和BeanFactoryPostProcessor后会自动调用它们的callback方法,不用用户显式的去调用.
分享到:
相关推荐
SpringContainer SpringContainer is a FrameLayout that supports overscroll on both top and bottom. Feel free to put any view(s) in. Based on that feature, it's very easy to do refreshing、 loading or ...
这是我的博客spring之扩展点的示例测试代码,不包含相关依赖包
Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点
史上最全spring以及扩展功能jar,包括了扩展的切面功能,额外的common-anotation,一次导入,永不报错!!!
4.Spring应用扩展.pptx
Spring社交扩展框架 Spring Social ,Spring Social 是 Spring 框架的扩展,用来方便开发Web社交应用程序,可通过该项...
Spring3.0.5支持AOP获取HttpServletResponse扩展资源:spring.web-3.0.5.jar和spring.webmvc-3.0.5.jar 是需要升级替换的。
NULL 博文链接:https://wangxinchun.iteye.com/blog/2301263
实现spring自定义扩展标签的实现步骤
- Spring 的扩展点主要有哪些? - Spring如何解决循环依赖? - 事务的传播行为是什么?有哪些? - 什么是AOP? - AOP的组成元素和概念有哪些? - AOP实现方式有哪些? - AspectJ AOP 和 Spring AOP的区别? - cglib...
Starting with the architecture of the Spring Framework and setting up the key components of the Spring application development environment, you will learn the configuration of Spring Container and how...
You will master powerful techniques like embedding a Spring container inside an OSGi bundle, and see how Spring's dependency injection compliments OSGi. Along the way, you'll learn to handle data ...
Spring文档,包括spring知识点总结,对初学者学习spring却有帮助
主要介绍了Springboot启动扩展点超详细教程小结,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
--Spring Boot内置的监控点、自定义的监控状况检查、自定义度量统计,输出等等 24 Spring Boot 测试54:13 --实战演示在Spring Boot里面的测试案例,以及如何测试Controller 25 Spring Boot 构建微服务实战01:01:50...
NULL 博文链接:https://liuyinhou.iteye.com/blog/1004484
主要介绍了如何利用Spring的@Import扩展点与spring进行无缝整合的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Starting with the architecture of the Spring Framework and setting up the key components of the Spring application development environment, you will learn the configuration of Spring Container and how...
三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot...