原文地址:http://blog.csdn.net/joenqc/article/details/66479154java
首先,这俩都是个接口…spring
实现 BeanFactory
接口的类代表此类事一个工厂,做用就是配置、新建、管理 各类Bean。app
而 实现 FactoryBean
的类代表此类也是一个Bean,类型为工厂Bean(Spring中共有两种bean,一种为普通bean,另外一种则为工厂bean)。顾名思义,它也是用来管理Bean的,而它自己由spring管理。ui
一个Bean想要实现 FactoryBean
,必须实现如下三个接口:spa
1. Object getObject():返回由FactoryBean建立的Bean的实例 2. boolean isSingleton():肯定由FactoryBean建立的Bean的做用域是singleton仍是prototype; 3. getObjectType():返回FactoryBean建立的Bean的类型。
有一点须要注意,若是将一个实现了FactoryBean的类成功配置到了spring上下文中,那么经过该类对象的名称(好比appleFactoryBean)从spring的applicationContext或者beanFactory获取bean时,获取到的是appleFactoryBean建立的apple实例,而不是appleFactoryBean本身,若是想经过spring拿到appleFactoryBean,须要在名称前加 &
符号 :.net
out.println(applicationContext.getBean("&appleFactoryBean"))
这个prefix在BeanFactory接口源码中有提到:prototype
/** * Used to dereference a {@link FactoryBean} instance and distinguish it from * beans <i>created</i> by the FactoryBean. For example, if the bean named * {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject} * will return the factory, not the instance returned by the factory. */ String FACTORY_BEAN_PREFIX = "&";
还有一点须要注意,FactoryBean管理的bean实际上也是由spring进行配置、实例化、管理,所以由FactoryBean管理的bean不能再次配置到spring配置文件中(xml、java类配置、注解均不能够),不然会报以下异常:code
Exception in thread "main" org.springframework.beans.factory.BeanIsNotAFactoryException: Bean named 'appleFactoryBean' is expected to be of type 'org.springframework.beans.factory.FactoryBean' but was actually of type 'java.lang.Object' at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1612) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84) at com.joen.testspringcontainer.Start.main(Start.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
附上一个例子:server
spring配置类:xml
@Configuration @ComponentScan public class Configurations { }
AppleBean :
//@Component 这里不能够加注解 !!!!!! public class AppleBean{ }
AppleFactoryBean :
@Component public class AppleFactoryBean implements FactoryBean{ public Object getObject() throws Exception { return new AppleBean(); } public Class<?> getObjectType() { return AppleBean.class; } public boolean isSingleton() { return false; } }
启动类 :
public class Start { public static void main(String[] args){ ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Configurations.class); out.println(applicationContext.getBean("appleFactoryBean"));//获得的是apple out.println(applicationContext.getBean("&appleFactoryBean"));//获得的是apple工厂 } }