标签:spring源码学习
入口方法:getSingleton,在
Object sharedInstance = getSingleton(beanName);
@Override
public Object getSingleton(String beanName) {
return getSingleton(beanName, true);
}
真正的实现:
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
singletonObject = this.earlySingletonObjects.get(beanName);
if (singletonObject == null && allowEarlyReference) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}
spring单例在同一个spring容器中只创建一次,之后在获取bean的时候,会首先尝试从缓存加载bean,首先从singletonObjects中获取,singletonObjects中存储的是BeanName->Bean Instance, 如果缓存为空,但该bean正在创建过程中(isSingletonCurrentlyInCreation)则尝试从singletonFactories中获取。这是因为spring创建单例bean的时候,存在循环依赖的问题。比如创建bean a的时候发现bean a引用了bean b,此时会去创建bean b,但又发现bean b引用了bean c,所以此时会去创建bean c,在创建bean c的过程中发现bean c引用bean a。这三个bean就形成了一个环。为了解决循环依赖的问题,spring采取了一种将创建的bean实例提早暴露加入到缓存中,一旦下一个bean创建的时候需要依赖上个bean,则直接使用ObjectFactory来获取bean。提前暴露bean实例到缓存的时机是在bean实例创建(调用构造方法)之后,初始化bean实例(属性注入)之前。具体在AbstractAutowireCapableBeanFactory类的
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {...}
方法中。在该方法中调用了DefaultSingletonBeanRegistry类的
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
synchronized (this.singletonObjects) {
if (!this.singletonObjects.containsKey(beanName)) {
this.singletonFactories.put(beanName, singletonFactory);
this.earlySingletonObjects.remove(beanName);
this.registeredSingletons.add(beanName);
}
}
}
将允许提前暴露的单例bean提前加入singletonFactories中,这样就可以在创建依赖的时候避免循环依赖问题。
在从singletonFactories获取bean后,会将其存储到earlySingletonObjects中,然后从singletonFactories移除该bean,之后在要获取该bean就直接从earlySingletonObjects获取。这是因为从singletonFactories获取bean过程中需要调用singletonFactory.getObject(),这里还有一些操作,这样可以进一步提升性能。缓存思想用的很多。在java里面缓存大多都是指一个map结构,我想这应该是map的get和put操作都是O(1),适合用作缓存。
spring bean加载相关的缓存有以下这些:
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of singleton factories: bean name --> ObjectFactory */
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<String, ObjectFactory<?>>(16);
/** Cache of early singleton objects: bean name --> bean instance */
private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
singletonObjects和earlySingletonObjects的区别主要在于earlySingletonObjects是为了解决循环依赖设置的,储存的是提前暴露的bean name –> bean instance,而singletonObjects存储的是完全实例化的bean name –> bean instance。
最后附上我看源码自己写的例子:首先定义了三个bean,
public class TestA {
private boolean beCallConstructor = false;
private TestB testB;
public TestA() {
beCallConstructor = true;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[TestB:").append(testB==null?"未初始化,":"已初始化,");
builder.append("是否调用完构造方法:").append(beCallConstructor?"是":"否").append("]");
return builder.toString();
}
public void setTestB(TestB testB) {
this.testB = testB;
}
}
public class TestB {
private boolean beCallConstructor = false;
private TestC testC;
public TestB() {
beCallConstructor = true;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[testC:").append(testC==null?"未初始化,":"已初始化,");
builder.append("是否调用完构造方法:").append(beCallConstructor?"是":"否").append("]");
return builder.toString();
}
public void setTestC(TestC testC) {
this.testC = testC;
}
}
public class TestC {
private boolean beCallConstructor = false;
private TestA testA;
public TestC() {
beCallConstructor = true;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[testA:").append(testA==null?"未初始化,":"已初始化,");
builder.append("是否调用完构造方法:").append(beCallConstructor?"是":"否").append("]");
return builder.toString();
}
public void setTestA(TestA testA) {
this.testA = testA;
}
}
测试方法:
public class TestCircle {
@Test
public void testCircle(){
ApplicationContext bf = new ClassPathXmlApplicationContext("testCircle.xml");
System.out.println(bf.getBean("testA"));
}
}
testA提前暴露在singletonFactories的快照
当testC引用了testA,此时直接从singletonFactories获取ObjectFactory,调用其getObject()方法获取提前暴露的testA,快照如下