场景引入
BeanFactory和FactoryBean有什么区别呢?
代码引入
代码一
我们先跑一跑这么一段代码,查看打印结果。
public class HelloService {
public String hello() {
return "hello spring~~~";
}
}
public class HelloServiceFactoryBean implements FactoryBean<HelloService> {
@Override
public HelloService getObject() throws Exception {
System.out.println("初始化了");
return new HelloService();
}
@Override
public Class<?> getObjectType() {
return HelloService.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
<bean id="helloService" class="com.test.ioc.factorybean.HelloServiceFactoryBean"></bean>
启动容器,获取上面这个“helloService”的bean, 得到一个什么样的结果呢?
// 容器启动完成并不会马上加载
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("ioc.xml");
// getBean才开始去加载这个对象,除非实现SmartFactoryBean接口并设置isEagerInit = true
Object helloService = ctx.getBean("helloService");
// 拿到了HelloService 而不是 HelloServiceFactoryBean
System.out.println(helloService.getClass().toString());
很神奇,我们用helloService这个beanName指向的是HelloServiceFactoryBean对象,实际得到的却是HelloService 这个对象。
代码二
我们再换一种写法。
ApplicationContext ctx = new ClassPathXmlApplicationContext("ioc.xml");
Object helloService = ctx.getBean("&helloService");
// 拿到了HelloServiceFactoryBean
System.out.println(helloService.getClass().toString());
在helloService前面加了一个&,得到了和之前完全不一样的结果——HelloServiceFactoryBean对象
对于这种神奇的现象,我们只能从源码里找答案了。
源码阅读
spring 容器的加载过程,从XML-> 循环解析读取标签-> registerBeanDefinition到Factory的Map,List里…然后工厂从Map里读取BeanDefinition实例化。
紧接着,实例化就到了下面preInstantiateSingletons() 这个方法。
public void preInstantiateSingletons() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<String>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
// 触发所有的非懒加载的 singleton beans 的初始化操作
for (String beanName : beanNames) {
// 合并父 Bean 中的配置,注意 <bean id="" class="" parent="" /> 中的 parent,用的不多
// 这里的继承和 java 语法中的继承没有任何关系
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// 非抽象、非懒加载的 singletons。如果配置了 'abstract = true',那是不需要初始化的
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
// 处理 FactoryBean
if (isFactoryBean(beanName)) {
// FactoryBean 的话,在 beanName 前面加上 ‘&’ 符号。
final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
boolean isEagerInit;
// 判断当前 FactoryBean 是否是 SmartFactoryBean 的实现
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
@Override
public Boolean run() {
return ((SmartFactoryBean<?>) factory).isEagerInit();
}
}, getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
// Trigger post-initialization callback for all applicable beans...
// 到这里说明所有的非懒加载的 singleton beans 已经完成了初始化
// 如果我们定义的 bean 是实现了 SmartInitializingSingleton 接口的,那么在这里得到回调
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
smartSingleton.afterSingletonsInstantiated();
return null;
}
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
其中有一个isFactoryBean()的判断,如果是的话,给beanName加一个“&”
final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
那么这个“&”有什么用的呢?
我们进入getBean()方法看看。
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
进入doGetBean方法有一个getObjectForBeanInstance
// 下面这个方法:如果是普通 Bean 的话,直接返回
// 如果是 FactoryBean 的话,返回它创建的那个实例对象
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
那么他怎么区分普通bean还是factorybean呢?
继续点进去查看。
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, RootBeanDefinition mbd) {
// Don't let calling code try to dereference the factory if the bean isn't a factory.
if (BeanFactoryUtils.isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
}
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
// 如果是普通bean,直接返回,通过他创建一个对象返回
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
Object object = null;
if (mbd == null) {
object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {
// Return bean instance from factory.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
if (mbd == null && containsBeanDefinition(beanName)) {
mbd = getMergedLocalBeanDefinition(beanName);
}
boolean synthetic = (mbd != null && mbd.isSynthetic());
// 从factorybean里获取bean
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
return object;
}
这有个isFactoryDereference方法
public static boolean isFactoryDereference(String name) {
// FACTORY_BEAN_PREFIX: 熟悉的 & 符号
return (name != null && name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
}
上半部分初始化了不为factoryBean的bean和&开头的bean。
这也解释了为什么我们在前面加个&能获得HelloServiceFactoryBean这个对象本身。
下半部分初始化factoryBean,通过 object = getObjectFromFactoryBean(factory, beanName, !synthetic);
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName); // Factory接口的getObject方法
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (object != null && shouldPostProcess) {
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily return non-post-processed object, not storing it yet..
return object;
}
beforeSingletonCreation(beanName);
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
afterSingletonCreation(beanName);
}
}
if (containsSingleton(beanName)) {
this.factoryBeanObjectCache.put(beanName, (object != null ? object : NULL_OBJECT));
}
}
}
return (object != NULL_OBJECT ? object : null);
}
}
else {
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (object != null && shouldPostProcess) {
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
return object;
}
}
主要是doGetObjectFromFactoryBean会去调用我们之前写的HelloServiceFactoryBean 的getObject的方法,所以返回的是一个HelloService对象。
// Factory接口的getObject方法
object = doGetObjectFromFactoryBean(factory, beanName);
@Override
public HelloService getObject() throws Exception {
System.out.println("初始化了");
return new HelloService();
}