Spring还有一些扩展的功能,比如,Spring允许通过两种后处理器对IOC容器进行扩展、提供了丰富完善的AOP支持以及可以定制定时器等。下面先讲述Spring的两种后处理器
一、Bean后处理器,主要是对容器中的其他Bean执行后处理,它是一个特殊的Bean,该Bean不对外提供服务,故无需定义id属性。
•必须实现BeanPostProcessor接口,并覆盖该接口中的两个方法:
Object postProcessAfterInitialization(Object bean,String beanname) throws BeanException;
Object postProcessBeforeInitialization(Object bean,String beanname) throws BeanException;
第一个方法在目标Bean初始化之前被调用,第二个方法在初始化之后被调用。
这两种方法中的第一个参数是系统即将进行后处理的Bean实例,第二个参数是该Bean实例的名称。
Bean后处理器应用实例
①在包下建立类MyBeanPost.java,代码编写如下
package org.beanpost;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPost implements BeanPostProcessor{
public Object postProcessAfterInitialization(Object bean, String beanname)
throws BeansException {
System.out.println("Bean后处理器在初始化之前对"+beanname+"进行处理");
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String beanname)
throws BeansException {
System.out.println("Bean后处理在初始化之后对"+beanname+"进行处理");
return bean;
}
}
②编写HelloWorld.java类,代码如下
package org.model; // 该类放在org.model包中
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
//该方法会在初始化过程中被执行
public void init(){
System.out.println("该句在Bean初始化过程中执行");
}
}
③编写配置文件applicationContext.xml,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 配置初始化方法属性,在初始化过程中就会执行init方法 -->
<bean id="HelloWorld" class="org.model.HelloWorld" init-method="init">
<property name="message" >
<value>Hello World!</value>
</property>
</bean>
<!-- 该Bean可以配置id属性,也可以不配置id属性 -->
<bean class="org.beanpost.MyBeanPost"></bean>
</beans>
④编写测试类HelloWorldTest.java,完成Bean的实例化,代码如下
package org.test;
import org.model.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloWorldTest {
public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml");
HelloWorld helloWorld=(HelloWorld) ac.getBean("HelloWorld");
System.out.println(helloWorld.getMessage());
}
}
运行程序,控制台输出信息如下:
Bean后处理在初始化之后对HelloWorld进行处理
该句在Bean初始化过程中执行
Bean后处理器在初始化之前对HelloWorld进行处理
Hello World!
二、容器后处理器,负责处理容器本身,它必须实现BeanFactoryPostProcessor接口,并覆盖其postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeanException方法。
①编写MyBeanFactoryPostProcessor.java,实现BeanFactoryPostProcessor接口,并覆盖其postProcessBeanFactory方法
package org.beanpost;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
System.out.println("程序同意Spring所做的BeanFactory的初始化");
}
}
②修改applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 配置初始化方法属性,在初始化过程中就会执行init()方法 -->
<bean id="HelloWorld" class="org.model.HelloWorld" init-method="init">
<property name="message" >
<value>Hello World!</value>
</property>
</bean>
<!-- 该Bean可以配置id属性,也可以不配置id属性 -->
<bean class="org.beanpost.MyBeanPost"></bean>
<bean id="beanfactorypost" class="org.beanpost.MyBeanFactoryPostProcessor"/>
</beans>
完成后,测试程序不变,控制台输出如下结果
程序同意Spring所做的BeanFactory的初始化
Bean后处理在初始化之后对HelloWorld进行处理
该句在Bean初始化过程中执行
Bean后处理器在初始化之前对HelloWorld进行处理
Hello World!
由于使用了ApplicationContext作为容器,容器会自动调用BeanFactoryPostProcessor来处理Spring容器。
但是如果使用BeanFactory作为容器,则必须手动调用容器后处理器来处理Spring容器。