自学内容网 自学内容网

2、spring5.2.x源码解读之IOC实现流程

1、代码调用入口

public static void main(String[] args) {
// 用我们的配置文件来启动一个 ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
// 从 context 中取出我们的 Bean,而不是用 new MessageServiceImpl() 这种方式
IMessageService messageService = context.getBean(IMessageService.class);
// 这句将输出: hello world
messageService.say("hello world");
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="messageService" class="org.springframework.test.web.test.service.impl.MessageServiceImpl"/>
<context:component-scan base-package="org.springframework.test.web.test" />
</beans>

2、IOC实现流程

在这里插入图片描述

public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {

super(parent);
setConfigLocations(configLocations);
if (refresh) {
//spring加载对象到map的入口
refresh();
}
}
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//为刷新容器做准备
prepareRefresh();

//获得刷新后的beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

//初始化beanFactory需要的一些参数
prepareBeanFactory(beanFactory);

try {
//后置处理bean工厂,接口实现类
postProcessBeanFactory(beanFactory);

//执行后置处理bean工厂
invokeBeanFactoryPostProcessors(beanFactory);

//注册后置处理bean
registerBeanPostProcessors(beanFactory);

//初始化容器的消息源
initMessageSource();

//初始化应用事件广播器
initApplicationEventMulticaster();

//在特殊容器初始化其它特殊bean
onRefresh();

//检查监听对象和注册监听对象
registerListeners();

// 初始化所有的 singleton beans
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
finishRefresh();
}

catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}

// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}

finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
//定制bean工厂
customizeBeanFactory(beanFactory);
//加载bean定义的配置文件,application.xml
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}

原文地址:https://blog.csdn.net/qq_38019655/article/details/140385244

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!