首先要分辨BeanFactory 與 FactoryBean的區別, 兩個名字很像,因此容易搞混html
BeanFactory: 以Factory結尾,表示它是一個工廠類,是用於管理Bean的一個工廠java
FactoryBean:以Bean結尾,表示它是一個Bean,不一樣於普通Bean的是:它是實現了FactoryBean<T>接 口的Bean,根據該Bean的Id從BeanFactory中獲取的其實是FactoryBean的getObject()返回的對象,而不是 FactoryBean自己, 若是要獲取FactoryBean對象,能夠在id前面加一個&符號來獲取。spring
Spring中的Bean有兩種。測試
一種是普通的bean ,好比配置this
[html] view plaincopyspa
<bean id="personService" class="com.spring.service.impl.PersonServiceImpl" scope="prototype"> .net
<property name="name" value="is_zhoufeng" /> prototype
</bean> 代理
那個使用BeanFactory根據id personService獲取bean的時候,獲得的對象就是PersonServiceImpl類型的。htm
另一種就是實現了org.springframework.beans.factory.FactoryBean<T>接口的 Bean , 那麼在從BeanFactory中根據定義的id獲取bean的時候,獲取的其實是FactoryBean接口中的getObject()方法返回的對 象。
以Spring提供的ProxyFactoryBean爲例子,配置以下:
[html] view plaincopy
<bean id="personServiceByLog" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.spring.service.PersonService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>logInteceptor</value>
<value>ZFMethodAdvice</value>
</list>
</property>
<property name="targetName" value="personService" />
</bean>
那麼在代碼中根據personServiceByLog來獲取的Bean其實是PersonService類型的。
[java] view plaincopy
public void test01() {
PersonService ps = context.getBean("personService", PersonService.class);
ps.sayHello();
String name = ps.getName();
System.out.println(name);
}
若是要獲取ProxyFactoryBean自己,能夠以下
[java] view plaincopy
public void test04() {
ProxyFactoryBean factoryBean = context.getBean("&personServiceByLog", ProxyFactoryBean.class);
PersonService ps = (PersonService) factoryBean.getObject();
String name = ps.getName();
System.out.println(name);
}
本身實現一個FactoryBean, 功能:用來代理一個對象,對該對象的全部方法作一個攔截,在方法調用先後都輸出一行log
[java] view plaincopy
package com.spring.factorybean;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
public class ZFFactoryBean implements FactoryBean<Object>, InitializingBean, DisposableBean {
// 被代理對象實現的接口名(在使用Proxy時須要用到,用於決定生成的代理對象類型)
private String interfaceName;
// 被代理的對象
private Object target;
// 生成的代理對象
private Object proxyObj;
public void destroy() throws Exception {
System.out.println("distory...");
}
public void afterPropertiesSet() throws Exception {
proxyObj = Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[] { Class.forName(interfaceName) }, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("method:" + method.getName());
System.out.println("Method before...");
Object result = method.invoke(target, args);
System.out.println("Method after...");
return result;
}
});
System.out.println("afterPropertiesSet");
}
public Object getObject() throws Exception {
System.out.println("getObject");
return proxyObj;
}
public Class<?> getObjectType() {
return proxyObj == null ? Object.class : proxyObj.getClass();
}
public boolean isSingleton() {
return true;
}
public String getInterfaceName() {
return interfaceName;
}
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
}
而後來試試:
首先這樣定義bean
[java] view plaincopy
<bean id="personService" class="com.spring.service.impl.PersonServiceImpl" scope="prototype">
<property name="name" value="is_zhoufeng" />
</bean>
<bean id="zfPersonService" class="com.spring.factorybean.ZFFactoryBean">
<property name="interfaceName" value="com.spring.service.PersonService" />
<property name="target" ref="personService"/>
</bean>
而後獲取Bean,並測試。
[java] view plaincopy
public void test06() {
PersonService ps = context.getBean("zfPersonService", PersonService.class);
ps.sayHello();
String name = ps.getName();
System.out.println(name);
}
會發現sayHello與getName方法調用先後都有log打印。
上面的ZFBeanFactory只是模仿了ProxyFactoryBean的功能作了一個實現而已。
其實經過FactoryBean這種特色,能夠實現不少有用的功能 。。。