上一篇讲到了BeanFactory
,那就不得不提一下FactoryBean
,先看一下它的定义吧!
定义
public interface FactoryBean<T> {
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
//返回的对象实例
@Nullable
T getObject() throws Exception;
//返回对象类型
@Nullable
Class<?> getObjectType();
//getObject返回的对象是否单例。true代表单例,false代表非单例
default boolean isSingleton() {
return true;
}
}
看到里面的三个方法名,我们可以见名知意了。Bean实例、Bean类型、bean是否单例。那接下来我们就实现FactoryBean 接口,看看是怎么使用 FactoryBean 的,然后再从源码的角度去看看 Spring 是怎么解析FactoryBean 中的 Bean 的。
实践
GongjFactoryBean
GongjFactoryBean 实现 FactoryBean ,并重写其三个方法。
@Component
public class GongjFactoryBean implements FactoryBean {
/**
* 当该Bean是单例时,该方法只执行一次
* @return
* @throws Exception
*/
@Override
public Object getObject() throws Exception {
User user = new User();
System.out.println("====" + user);
return user;
}
@Override
public Class<?> getObjectType() {
return User.class;
}
/**
* 可以控制该Bean的作用域是单例还是原型
* @return
*/
@Override
public boolean isSingleton() {
return true;
}
}
AppConfig
新建AppConfig作为启动扫描类
@ComponentScan("com.gongj")
public class AppConfig {
}
Main
使用 AnnotationConfigApplicationContext 容器启动 Spring
public class Main {
public static void main(String[] args) {
//启动spring容器 ----> 创建非懒加载的单例Bean
//扫描包路径
AnnotationConfigApplicationContext annotationConfigApplicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println(annotationConfigApplicationContext.getBean("gongjFactoryBean")); System.out.println(annotationConfigApplicationContext.getBean("gongjFactoryBean"));
System.out.println(annotationConfigApplicationContext.getBean("&gongjFactoryBean"));
System.out.println(annotationConfigApplicationContext.getBean("&gongjFactoryBean"));
}
}
启动main方法输出以下结果:
====com.gongj.entity.User@cac736f #这是我在GongjFactoryBean中进行打印的
com.gongj.entity.User@cac736f
com.gongj.entity.User@cac736f
com.gongj.service.GongjFactoryBean@5e265ba4
com.gongj.service.GongjFactoryBean@5e265ba4
通过上面启动的这个例子我们可以得出以下几个结论:
- GongjFactoryBean#getObject()函数只执行一次
- 根据 gongjFactoryBean 获取的是 User 对象并每次获取的 User 对象一致。
- 根据 &gongjFactoryBean 获取的是 GongjFactoryBean 对象并每次获取的 GongjFactoryBean 对象一致。
初步怀疑该现象是 isSingleton
函数进行控制的。那我接下来将isSingleton
函数返回的值修改为false
。
@Override
public boolean isSingleton() {
return false;
}
再次执行main方法,输出以下结果:
====com.gongj.entity.User@cac736f #这是我在GongjFactoryBean中进行打印的
com.gongj.entity.User@cac736f
====com.gongj.entity.User@5e265ba4 #这是我在GongjFactoryBean中进行打印的
com.gongj.entity.User@5e265ba4
com.gongj.service.GongjFactoryBean@156643d4
com.gongj.service.GongjFactoryBean@156643d4
通过上面启动的这个例子我们又可以得出以下几个结论:
- isSingleton() 函数控制的是以
gongjFactoryBean
获取的对象是否是单例的。 - isSingleton() 函数的修改不影响以
&gongjFactoryBean
获取的对象,它还是单例的。
看到这我们应该就有疑惑了,让我们带着疑惑开始一步一步DEGUG源码。
将 isSingleton() 函数的返回值修改为true。
根据上图我们可以得出结论:
- GongjFactoryBean 在启动的时候就已经加入到 Spring 容器中。很显然 GongjFactoryBean 是一个非懒加载的单例 Bean。
调试
- 进入
AbstractApplicationContex
t#getBean
@Override
public Object getBean(String name) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name); 》》
}
-
AbstractBeanFactory
#getBean
@Override
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false); 》》
}
-
AbstractBeanFactory
#doGetBean
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
// 对beanName进行转换 name如果是"&gongjFactoryBean",那么beanName就是"gongjFactoryBean"
final String beanName = transformedBeanName(name); 》》
Object bean;
//根据beanName去单例池中获取单例Bean
Object sharedInstance = getSingleton(beanName); 》》
if (sharedInstance != null && args == null) {
//.....省略代码
// 判断sharedInstance是不是FactoryBean,
// 如果是FactoryBean,那么真正需要拿到的是getObject方法所返回的对象
//beanName 是spinrg进行解析后获取到的BeanName
//name 我们手动传入的beanName
//sharedInstance 根据beanName获取到的单例Bean对象
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); 》》
}
else{
//....省略代码 下篇分析
}
}
-
AbstractBeanFactory
#transformedBeanName
先调用BeanFactoryUtils.transformedBeanName
方法,然后拿到返回值,再执行canonicalName
方法。
//返回beanName,去掉传入name的&前缀,并且把返回的name当做别名去aliasMap中寻找原始的beanName
protected String transformedBeanName(String name) {
return canonicalName(BeanFactoryUtils.transformedBeanName(name));
}
BeanFactoryUtils#transformedBeanName
private static final Map<String, String> transformedBeanNameCache = new ConcurrentHashMap<>();
String FACTORY_BEAN_PREFIX = "&";
public static String transformedBeanName(String name) {
Assert.notNull(name, "'name' must not be null");
// 如果beanName没有以&开头,则直接返回
if (!name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) { // gongjFactoryBean
return name;
}
// 如果beanName以&开头,截取&后的beanName,并且把截取前后的name存在transformedBeanNameCache 中
//可能会有多个&&&,循环截取
return transformedBeanNameCache.computeIfAbsent(name, beanName -> {
do {
beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
}
while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
return beanName;
});
}
SimpleAliasRegistry#canonicalName
private final Map<String, String> aliasMap = new ConcurrentHashMap<>(16);
public String canonicalName(String name) {
String canonicalName = name;
String resolvedName;
do {
//根据传递过来的beanName,去别名Map中查找 获取出来就是真正的BeanName 例如:gjfg = gongjFactoryBean
resolvedName = this.aliasMap.get(canonicalName);
if (resolvedName != null) {
canonicalName = resolvedName;
}
}
while (resolvedName != null);
return canonicalName;
}
transformedBeanName
方法到此结束!
- getSingleton
再来看一下 Spring 是如何根据 beanName 去单例池中获取单例Bean
@Override
@Nullable
public Object getSingleton(String beanName) {
return getSingleton(beanName, true);
}
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
// 从单例池中获取实例 singletonObjects 一级缓存
Object singletonObject = this.singletonObjects.get(beanName);
// 如果从单例池中没有获取到实例 并且指定的单例bean正在创建中
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
//锁定全局变量并进行处理
synchronized (this.singletonObjects) {
//earlySingletonObjects 二级缓存
singletonObject = this.earlySingletonObjects.get(beanName);
// 没有从二级缓存中获取到
if (singletonObject == null && allowEarlyReference) {
//当某些方法需要提前初始化的时候,会调 addSingletonFactory 方法将对
//ObjectFactory 初始化存储在 singletonFactories Map中
// singletonFactories 三级缓存
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//记录在缓存中 earlySingletonObjects与singletonFactories互斥
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
这个方法首先尝试从 singletonObjects(一级缓存)
里面获取实例,如果取不到再从earlySingletonObjects(二级缓存)
里面获取,如果还获取不到,再尝试从 singletonFactories(三级缓存)
里面获取 beanName 对应的 ObjectFactory
,然后调用这个 ObjectFactory
的 getObject 方法来获取 bean ,并放到 earlySingletonObjects(二级缓存)
里面 ,并且从singletonFacotories(三级缓存)
里面 remove 掉这个 ObjectFactorγ
。
- singletonObjects :单例对象的缓存:Bean名称到Bean实例,也就是常说的单例池(一级缓存)。
- singletonFacotories :单例工厂的缓存(三级缓存), Bean名称到ObjectFactory,一旦最终对象被创建(通过objectFactory.getObject()),此引用信息将删除
- earlySingletonObjects :用于存储在创建Bean早期对创建的原始bean的一个引用(二级缓存),注意这里是原始bean,即使用工厂方法或构造方法创建出来的对象,一旦对象最终创建好,此引用信息将删除
getSingleton 方法到此结束!
-
AbstractBeanFactory#getObjectForBeanInstance
关键部分就在这个方法中,进入方法代码
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
// name也就是我传入的beanName 如果是以&符号开头,则直接返回单例池(SingletonObjects)中的对象
if (BeanFactoryUtils.isFactoryDereference(name)) {
if (beanInstance instanceof NullBean) {
return beanInstance;
}
//判断单例对象beanInstance是否实现了FactoryBean,如果没有抛出异常
if (!(beanInstance instanceof FactoryBean)) {
throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
}
if (mbd != null) {
mbd.isFactoryBean = true;
}
// 返回单例池(SingletonObjects)中的对象
return beanInstance;
}
// 如果 beanInstance 不是 FactoryBean,则直接返回
if (!(beanInstance instanceof FactoryBean)) {
return beanInstance;
}
Object object = null;
if (mbd != null) {
mbd.isFactoryBean = true;
}
else {
//根据真正的BeanName从factoryBeanObjectCache缓存中获取Bean对象
object = getCachedObjectForFactoryBean(beanName);
}
// 从factoryBeanObjectCache中没有拿到则进行创建
if (object == null) {
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
//从beanDefinitionMap判断该Bean是否存在
if (mbd == null && containsBeanDefinition(beanName)) {
//进行Bean合并
mbd = getMergedLocalBeanDefinition(beanName);
}
//是否是用户定义的而不是应用程序本身定义的
boolean synthetic = (mbd != null && mbd.isSynthetic());
// 调用getObject方法得到对象
object = getObjectFromFactoryBean(factory, beanName, !synthetic); 》》
}
return object;
}
FactoryBeanRegistrySupport#getObjectFromFactoryBean
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
// 是不是单例的 && 根据 beanName 判断该名称在 singletonObjects(单例池) 中是否存在
if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
// 调用getObject方法得到一个对象
object = doGetObjectFromFactoryBean(factory, beanName); 》》
//再从缓存中获取一遍
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (shouldPostProcess) {
//返回指定的单例bean当前是否正在创建中
if (isSingletonCurrentlyInCreation(beanName)) {
return object;
}
//表示这些bean正常创建中,在没创建完时不能重复创建
beforeSingletonCreation(beanName);
try {
// 调用BeanPostProcessor执行初始化后的逻辑,主要就是进行AOP
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)) {
//将创建的Bean放入factoryBeanObjectCache
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
return object;
}
}
else {
// 多例 每次都创建一个,不从factoryBeanObjectCache中获取
Object object = doGetObjectFromFactoryBean(factory, beanName);
if (shouldPostProcess) {
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);
}
}
return object;
}
}
-
FactoryBeanRegistrySupport#doGetObjectFromFactoryBean
在doGetObjectFromFactoryBean
方法中我们终于看到了我们想要看到的方法,也就是object = factory.getObject();
private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
throws BeanCreationException {
Object object;
try {
if (System.getSecurityManager() != null) {
AccessControlContext acc = getAccessControlContext();
try {
object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//调用了FectoryBean里的getObject()函数
object = factory.getObject();
}
}
catch (FactoryBeanNotInitializedException ex) {
throw new BeanCurrentlyInCreationException(beanName, ex.toString());
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
}
// 如果调用getObject()方法返回的是null,那么则返回一个NullBean
if (object == null) {
if (isSingletonCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(
beanName, "FactoryBean which is currently in creation returned null from getObject");
}
object = new NullBean();
}
return object;
}
到此,FactoryBean
就结束啦!当然 getBean 方法才看到冰山一角。
FactoryBean
是一个能生产或修饰对象生成的工厂 Bean。一个 Bean 如果实现了 FactoryBean
接口,那么根据该 Bean 的名称获取到的实际上是 getObject()
返回的对象,而不是这个 Bean 自身实例,如果要获取这个Bean自身实例,那么需要在名称前面加上'&'
符号。