打上断点进行调试
1 第一步是进入了ClassPathXmlApplicationContext调用其构造参数,其中配置文件的内容被解析成了数组
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[]{configLocation}, true, (ApplicationContext)null);
}
//上面的构造器内部调用的就是以下这个构造器
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
//自动配置文件所在的位置
this.setConfigLocations(configLocations);
if (refresh) {
//此处所有的单实例bean创建完成,我们继续进入refresh方法内进行研究
this.refresh();
}
}
//3 第三步用来分析refresh(),此处所有的单实例bean创建完成
public void refresh() throws BeansException, IllegalStateException {
//this.startupShutdownMonitors是类中定义的一个用专门于同步的锁对象,同步多线程ioc容器的创建于销毁,即保证多线程下ioc只能被创建一次
synchronized(this.startupShutdownMonitor) {
//刷新容器
this.prepareRefresh();
//通过解析spring.xml配置文件创建了beanFactory对象,它的内部获取了组件的基本信息并保存在了beanDefinitionMap中,通过它来创建组件对象,解析的原理主要就是通过反射
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
//此时bean工厂已准备好,配置文件已被解析
this.prepareBeanFactory(beanFactory);
try {
//加载bean工厂的后置处理器
this.postProcessBeanFactory(beanFactory);
//执行beanFactory的后置处理器
this.invokeBeanFactoryPostProcessors(beanFactory);
//注册bean工厂的后置处理器
this.registerBeanPostProcessors(beanFactory);
//用于支持国际化功能
this.initMessageSource();
//初始化容器多事件转发器
this.initApplicationEventMulticaster();
//留给子类(开发中)自己写容器时进行扩展实现
this.onRefresh();
//注册spring内部监听器
this.registerListeners();
//完成beanFactory的初始化 => 所有单实例对象加载完毕,我们需要对该方法进一步研究
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var9) {
......
} finally {
......
}
}
}
该方法可以理解为初始化所有单实例bean
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
if (beanFactory.containsBean("conversionService") && beanFactory.isTypeMatch("conversionService", ConversionService.class)) {
beanFactory.setConversionService((ConversionService)beanFactory.getBean("conversionService", ConversionService.class));
}
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver((strVal) -> {
return this.getEnvironment().resolvePlaceholders(strVal);
});
}
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
String[] var3 = weaverAwareNames;
int var4 = weaverAwareNames.length;
for(int var5 = 0; var5 < var4; ++var5) {
String weaverAwareName = var3[var5];
this.getBean(weaverAwareName);
}
beanFactory.setTempClassLoader((ClassLoader)null);
beanFactory.freezeConfiguration();
//主要研究这个放
beanFactory.preInstantiateSingletons();
}
只要不是抽象bean,不是多实例bean,不是懒加载,就通过this.getBean(beanName);
获取对象
public void preInstantiateSingletons() throws BeansException {
//日志处理
if (this.logger.isTraceEnabled()) {
this.logger.trace("Pre-instantiating singletons in " + this);
}
//拿到所有bean组件的名字放到list中然后继续遍历,其中beanDefinitionNames中具有容器中全部组件的详细信息,如名字、作用域、是否抽象....
List<String> beanNames = new ArrayList(this.beanDefinitionNames);
Iterator var2 = beanNames.iterator();
//顺序遍历bean组件,所以实际开发中bean对象的创建顺序与它们在容器中的顺序一致
while(true) {
String beanName;
Object bean;
do {
while(true) {
RootBeanDefinition bd;
do {
do {
do {
if (!var2.hasNext()) {
var2 = beanNames.iterator();
while(var2.hasNext()) {
beanName = (String)var2.next();
Object singletonInstance = this.getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton)singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(() -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, this.getAccessControlContext());
} else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
return;
}
beanName = (String)var2.next();
bd = this.getMergedLocalBeanDefinition(beanName);
//创建条件1:组件不能是抽象的
} while(bd.isAbstract());
//创建条件1:组件不能是抽象的
} while(!bd.isSingleton());
} while(bd.isLazyInit());
if (this.isFactoryBean(beanName)) {
bean = this.getBean("&" + beanName);
break;
}
this.getBean(beanName);
}
/*
FactoryBean是Spring中工厂模式应用案例,继承FactoryBean的pojo类返回的不是该类的pojo对象,
而是pojo内部重写的FactoryBean的getObject方法中返回的对象
*/
} while(!(bean instanceof FactoryBean));
FactoryBean<?> factory = (FactoryBean)bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
SmartFactoryBean var10000 = (SmartFactoryBean)factory;
((SmartFactoryBean)factory).getClass();
isEagerInit = (Boolean)AccessController.doPrivileged(var10000::isEagerInit, this.getAccessControlContext());
} else {
isEagerInit = factory instanceof SmartFactoryBean && ((SmartFactoryBean)factory).isEagerInit();
}
if (isEagerInit) {
this.getBean(beanName);
}
}
}
//调用getBean实际就是调用AbstractBeanFactory子类的doGetBean方法
public Object getBean(String name) throws BeansException {
return this.doGetBean(name, (Class)null, (Object[])null, false);
}
protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly) throws BeansException {
//获取组件名
String beanName = this.transformedBeanName(name);
/*
先从已注册的单实例bean中,也就是单例池中,查看有无这个组件,没有就先创建,后续再次创建直接从单例池中拿即可
*/
Object sharedInstance = this.getSingleton(beanName);
Object bean;
bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, (RootBeanDefinition)null);
} else {
if (this.isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
BeanFactory parentBeanFactory = this.getParentBeanFactory();
if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) {
String nameToLookup = this.originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory)parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);
}
if (args != null) {
return parentBeanFactory.getBean(nameToLookup, args);
}
if (requiredType != null) {
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
return parentBeanFactory.getBean(nameToLookup);
}
if (!typeCheckOnly) {
//标记bean已经被创建
this.markBeanAsCreated(beanName);
}
try {
RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
this.checkMergedBeanDefinition(mbd, beanName, args);
//拿到这个bean的依赖bean并先于该bean创建这个组件
String[] dependsOn = mbd.getDependsOn();
String[] var11;
//循环创建这个bean所依赖的组件对象
if (dependsOn != null) {
var11 = dependsOn;
int var12 = dependsOn.length;
for(int var13 = 0; var13 < var12; ++var13) {
String dep = var11[var13];
if (this.isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
this.registerDependentBean(dep, beanName);
try {
this.getBean(dep);
} catch (NoSuchBeanDefinitionException var24) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", var24);
}
}
}
//如果是单例模式
if (mbd.isSingleton()) {
//创建单例bean
sharedInstance = this.getSingleton(beanName, () -> {
try {
return this.createBean(beanName, mbd, args);
} catch (BeansException var5) {
this.destroySingleton(beanName);
throw var5;
}
});
bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
//真正的创建组件对象的方法
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized(this.singletonObjects) {
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (this.logger.isDebugEnabled()) {
this.logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
this.beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = this.suppressedExceptions == null;
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet();
}
try {
//此处获取到单例bean对象
singletonObject = singletonFactory.getObject();
newSingleton = true;
} catch (IllegalStateException var16) {
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
throw var16;
}
} catch (BeanCreationException var17) {
BeanCreationException ex = var17;
if (recordSuppressedExceptions) {
Iterator var8 = this.suppressedExceptions.iterator();
while(var8.hasNext()) {
Exception suppressedException = (Exception)var8.next();
ex.addRelatedCause(suppressedException);
}
}
throw ex;
} finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
this.afterSingletonCreation(beanName);
}
//如果是新建的单例对象,就将其加入到单例池中
if (newSingleton) {
this.addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}