bean后處理器和容器后處理器
1.bean后處理器
這種處理器會(huì)對容器中的bean進(jìn)行后處理,對bean的功能進(jìn)行額外加強(qiáng)
bean的后處理器必須實(shí)現(xiàn)接口BeanPostProcessor,該接口包含兩個(gè)方法
public Object postProcessBeforeInitialization(Object
bean, String beanName)
public Object postProcessAfterInitialization(Object
bean, String beanName)
下面定義一個(gè)bean后處理器
import org.springframework.beans.BeansException;
import
org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements
BeanPostProcessor {
public Object
postProcessBeforeInitialization(Object bean,
String beanName)
throws BeansException {
System.out.println(bean);
System.out.println("bean后處理器在bean初始化之前對"+beanName+"進(jìn)行增強(qiáng)處理!");
return
bean; }
public Object
postProcessAfterInitialization(Object bean, String
beanName)
throws
BeansException {
System.out.println("bean后處理器在bean初始化之后對"+beanName+"進(jìn)行增強(qiáng)處理!");
if(bean instanceof
American){
American a =
(American)bean;
a.setName("后處理器重新設(shè)置的值");
} return
bean;
}
下面是測試使用的bean類
斧頭的接口
public interface Axe {
String chop();
}
普通人的接口
public interface Person {
void useAxe();
}
斧頭的實(shí)現(xiàn)類
import com.spring.being.Axe;
public class SteelAxe implements Axe {
private int count;
public SteelAxe(){
System.out.println("SteelAxe的無參構(gòu)造器被調(diào)用......");
}
public String chop() {
return
"鐵斧用了"+(++count)+"次";
}
}
普通人的實(shí)現(xiàn)類(實(shí)現(xiàn)接口Person和InitializingBean )
import org.springframework.beans.factory.InitializingBean;
import com.spring.being.Axe;
import com.spring.being.Person;
public class American
implements Person,InitializingBean {
private Axe axe;
private String name;
public American(){
System.out.println("Chinese的無參構(gòu)造器被調(diào)用.......");
}
public American(Axe axe){
this.axe=axe;
System.out.println("Chinese的有參構(gòu)造器被調(diào)用.......");
}
//public abstract Axe
createAxe();//設(shè)置一個(gè)抽象類,該方法由spring負(fù)責(zé)實(shí)現(xiàn)
public void setAxe(Axe axe) {
System.out.println("Spring執(zhí)行依賴關(guān)系注入.......");
this.axe = axe;
}
public Axe getAxe() {
return axe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void useAxe() {
System.out.println("美國人使用:"+axe.chop());
System.out.println("參數(shù)name:"+name);
}
public void
init(){
//查看bean后處理器是在bean初始化之前還是之后起作用的呢,書寫該方法該類必須在配置市加上屬性init-method="init",方法名是任意的,只要一致就行
System.out.println("正在執(zhí)行初始化方法init......");
}
public void afterPropertiesSet()
throws Exception {
//查看bean后處理器是在bean初始化之前還是之后起作用的呢,書寫該方法該類必須實(shí)現(xiàn)接口InitializingBean
System.out.println("正在執(zhí)行初始化方法afterPropertiesSet......");
}
}
書寫配置文件
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www./schema/beans"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://www./schema/beans
http://www./schema/beans/spring-beans-2.5.xsd">
<bean id="axe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="steelAxe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="American"
class="com.spring.postprocessor.American"
init-method="init">
<property
name="axe" ref="steelAxe" />
<property
name="name" value="依賴注入的值"/>
</bean>
<!--配置bean后處理器 -->
<bean
id="beanPostProcessor"
class="com.spring.postprocessor.MyBeanPostProcessor"
/> </beans>
書寫一個(gè)測試類
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");//用該方法加載可以自動(dòng)注冊bean后處理器,否則需要手動(dòng)注冊
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
測試結(jié)果:
bean后處理器在bean初始化之前對American進(jìn)行增強(qiáng)處理! 正在執(zhí)行初始化方法afterPropertiesSet......
正在執(zhí)行初始化方法init......
bean后處理器在bean初始化之后對American進(jìn)行增強(qiáng)處理!
美國人使用:鐵斧用了1次
參數(shù)name:后處理器重新設(shè)置的值 //bean的參數(shù)值已被后處理器修改了
spring提供的兩種常用的后處理器
BeanNameAutoProxyCreator
DefaultAdvisorAutoProxyCreator:根據(jù)提供的Advisor,對容器中的所有bean創(chuàng)建代理
2.容器后處理器
容器必須實(shí)現(xiàn)接口BeanFactoryPostProcessor,該接口有方法:
public void
postProcessBeanFactory(ConfigurableListableBeanFactory
beanFactory) throws
BeansException
書寫一個(gè)容器后處理器:
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("本程序?qū)pring所做的beanfactory的初始化沒有意見。。。。");
System.out.println("spring容器是:"+beanFactory);
System.out.println("可以取得bean:"+beanFactory.getBean("American"));
}
}
配置文件
在上面的配置文件再加上
<!--配置容器后處理器 -->
<bean id="beanFactoryPostProcessor"
class="com.spring.postprocessor.MyBeanFactoryPostProcessor"
/>
測試類
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
輸出結(jié)果:
本程序?qū)pring所做的beanfactory的初始化沒有意見。。。。
spring容器是:org.springframework.beans.factory.suppor
spring提供的幾種常見的容器后處理器
1.PropertyPlaceholderConfigurer屬性占位符配置器
2.PropertyOverrideConfigurer重寫占位符配置器
對PropertyPlaceholderConfigurer的測試如下:
配置文件applicationContext-deppen.xml:
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www./schema/beans"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://www./schema/beans
http://www./schema/beans/spring-beans-2.5.xsd">
<bean id="axe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="steelAxe"
class="com.spring.being.impl.SteelAxe"
scope="prototype"/>
<bean id="American"
class="com.spring.postprocessor.American"
init-method="init">
<property
name="axe" ref="steelAxe" />
<property name="name"
value="${person.name}"
/> </bean>
<!-- 配置屬性占位符配置器 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>person.properties</value>
</list>
</property>
</bean>
</beans>
配置文件person.properties:
person.name=\u4F9D\u8D56\u6CE8\u5165\u7684\u503C
測試類:
import org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.being.Person;
public class Test {
public static void main(String[] args) {
BeanFactory factory = new
ClassPathXmlApplicationContext("applicationContext-deppen.xml");
Person p =
(Person)factory.getBean("American");
p.useAxe();
}
}
輸出結(jié)果:
Chinese的無參構(gòu)造器被調(diào)用.......
SteelAxe的無參構(gòu)造器被調(diào)用......
Spring執(zhí)行依賴關(guān)系注入.......
正在執(zhí)行初始化方法afterPropertiesSet......
正在執(zhí)行初始化方法init......
美國人使用:鐵斧用了1次
參數(shù)name:依賴注入的值
|