Spring中BeanFactory和FactoryBean的区别:
1、BeanFactory
BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其他具体的IOC容器提供了最基本的规范,例如ApplicationContext,XmlBeanFactory 等具体的容器都是实现了BeanFactory,再在其基础之上附加了其他的功能。
BeanFactory源码:
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);
}
2、FactoryBean
FactoryBean是一个接口,当在IOC容器中的Bean实现了FactoryBean后,通过getBean(String BeanName)获取到的Bean对象并不是FactoryBean的实现类对象,而是这个实现类中的getObject()方法返回的对象。要想获取FactoryBean的实现类,就要getBean(&BeanName),在BeanName之前加上&。
FactoryBean源码:
package org.springframework.beans.factory;
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
下面是一个应用FactoryBean的例子:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="student" class="com.sg.pojo.Student">
<property name="name" value="zhangsan"/>
</bean>
<bean id="school" class="com.sg.pojo.Teacher">
<property name="name" value="lisi"/>
</bean>
<bean id="myFactoryBean" class="com.sg.pojo.MyFactoryBean">
<property name="type" value="student"/>
</bean>
</beans>
MyFactoryBean的实现类:
import org.springframework.beans.factory.FactoryBean;
public class MyFactoryBean implements FactoryBean{
private String type;
@Override
public Object getObject() throws Exception {
if("student".equals(type)){
return new Student();
}else{
return new Teacher();
}
}
@Override
public Class getObjectType() {
return School.class;
}
@Override
public boolean isSingleton() {
return true;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
测试类:
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.bean.FactoryBeanPojo;
public class MyFactoryBeanTest {
public static void main(String[] args){
ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
Object student= application.getBean("myFactoryBean");
MyFactoryBeanPojo myFactoryBean= (MyFactoryBeanPojo)application.getBean("&myFactoryBean");
System.out.println(student.getClass().getName());
System.out.printlnmyFactoryBean.getClass().getName());
}
}
输出结果:
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@35b793ee: defining beans [student,teacher,myFactoryBean]; root of factory hierarchy
com.sg.pojo.Student
com.sg.pojo.MyFactoryBean
从结果上可以看到当从IOC容器中获取FactoryBeanPojo对象的时候,用getBean(String BeanName)获取的确是Student对象,可以看到在MyFactoryBean中的type属性设置为student的时候,会在getObject()方法中返回Student对象。所以说从IOC容器获取实现了FactoryBean的实现类时,返回的却是实现类中的getObject方法返回的对象,要想获取FactoryBean的实现类,得在getBean(String BeanName)中的BeanName之前加上&,写成getBean(String &BeanName)。
3、BeanFactory和FactoryBean的区别
BeanFactory是提供了IOC容器最基本的形式,给具体的IOC容器的实现提供了规范,FactoryBean可以说为IOC容器中Bean的实现提供了更加灵活的方式,FactoryBean在IOC容器的基础上给Bean的实现加上了一个简单工厂模式和装饰模式,我们可以在getObject()方法中灵活配置。
本文详细介绍了Spring中BeanFactory作为IOC容器的基础接口,以及FactoryBean作为一个特殊接口,使得在IOC容器中管理的Bean可以有更灵活的创建方式。BeanFactory提供基本的bean管理和获取,而FactoryBean则在getBean时返回getObject方法的对象,若想获取FactoryBean本身需使用'&'前缀。通过一个实例展示了如何在配置文件中使用FactoryBean,并通过测试验证了其工作原理。

被折叠的 条评论
为什么被折叠?



