1.概述
IOC 即 Inversion of Control 中文 控制反转,它是一种软件设计思想,怎么理解这种思想呢?
- 字面上无非是两部分 控制 反转
- 控制了谁?
控制(管理)了对象 - 反转是什么?
正常开发过程中,如果一个对象A引用了另一个对象B,我们需要在A中去new一个B,那么相对应的这里的反转是指,所依赖的对象B直接注入到对象A中,依赖的对象由原来的主动获取变为被动接受,即 获得依赖对象的过程被反转了,故称之为反转。
对于某个具体的对象而言,以前是它控制其他对象,现在是所有对象都被spring控制,也可以理解为控制反转。
2.Spring的IOC实现原理简述
2.1 怎样去控制对象呢?
Spring通过容器BeanFactory的 实现 来控制对象,容器可以看做是一个大map,通过key-value的形式,将扫描到的bean实例化后存放到BeanFactory容器中,从而实现了对象的管理。
2.2 反转是怎么实现的呢?
容器通过对bean的注册,管理了bean,而做反转则需要维护bean与bean之间的关系,这种关系的实现是通过DI(Dependency Injection ,依赖注入)的方式,在运行期间 ,动态的维护了对象之间的关系。
3.Spring IOC实现方式
- 1.通过xml方式实现----这里就不举例,大家自行脑补。
- 2.通过注解方式实现 —如@Component、@Service等
4.小结
IOC 的Spring 实现就是通过容器技术管理bean,利用用依赖注入的方式,实现对象之间的解耦(或者是反转)。依赖注入是实现spring框架实现控制反转的一种方式。
5.实现原理深入解析—几个重要的类
5.1 BeanFactory接口
是Spring容器的最顶层接口,定义了容器的基本规范,所以它也是一个低配版的容器,该容器定义了如 getBean,getType,containsBean等方法。其代码如下:
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String var1) throws BeansException;
<T> T getBean(String var1, Class<T> var2) throws BeansException;
Object getBean(String var1, Object... var2) throws BeansException;
<T> T getBean(Class<T> var1) throws BeansException;
<T> T getBean(Class<T> var1, Object... var2) throws BeansException;
<T> ObjectProvider<T> getBeanProvider(Class<T> var1);
<T> ObjectProvider<T> getBeanProvider(ResolvableType var1);
boolean containsBean(String var1);
boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;
boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;
@Nullable
Class<?> getType(String var1) throws NoSuchBeanDefinitionException;
String[] getAliases(String var1);
}
5.2 ApplicationContext接口
相对于BeanFactory相比,ApplicationContext接口是高配版的IOC容器,其除了继承了BeanFactory接口,还继承了其他的接口提供了更强大的功能。其类图如下,可以看到他通过继承HierarchicalBeanFactory和ListableBeanFactory间接的继承了BeanFactory
- 继承了EnvironmentCapable 用于获取Environment实例
- 继承了HierarchicalBeanFactory 实现了Bean工厂的分层
BeanFactory getParentBeanFactory();//获取父工厂
boolean containsLocalBean(String name);//根据beanName来查询,查询本地容器是否包含Bean
- 继承了ListableBeanFactory,拥有了一系列关于获取bean(返回bean的名称或数组而非对象)的方法
- 继承了MessageSource,提供了国际化(“i18n”)功能。
- 继承了ApplicationEventPublisher,提供了消息发布功能。
- 继承了ResourcePatternResolver,提供了关于解析资源文件的功能。
它本身又扩展了以下几个方法
@Nullable
String getId();
String getApplicationName();
String getDisplayName();
long getStartupDate();
@Nullable
ApplicationContext getParent();
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
5.3 BeanDefinition接口
有了容器,那我我们就可以对bean进行管理了,但每个bean都有不同的类名、属性、类型及是否单例等信息。BeanDefinition接口就是对javaBean定义的抽象,spring将javaBean的数据结构转化为内存中的BeanDefinition数据结构进行维护。
它继承了一下两个接口
- AttributeAccessor 主要提供了设置属性,获得属性,及判断属性是否存在,及列出所有属性的一系列方法
- BeanMetadataElement 提供了 用于返回此元数据元素的配置源对象的方法
@Nullable
default Object getSource() {
return null;
}
但最重要的是它本身定义了相当多的方法,包括但不限于:
- 设置及获取其Scope ,区分是否为单例还是多例模式
- 是否启用延迟加载
- 设置及获取依赖的bean的名称
- 设置及获取是否是主自动装配候选对象
void setPrimary(boolean primary);
- 是否是抽象类
- 获取当前对象的容器BeanFactory
- 获取构造方法相关信息
- 获取初始化(init)方法名及销毁(Destroy)方法名
6.创建过程
Spring容器实现包括很多,这里以常用的FileSystemXmlApplicationContext为例,分析其启动过程
6.1 概括
- 1.创建BeanFactory
- 2.实例化Bean对象
6.2前提环境
主代码如下:
//初始化容器
ApplicationContext applicationContext =
new FileSystemXmlApplicationContext("classpath:spring-chapter2.xml");
//从容器中获取名字为user的bean
TstService tstService =
(TstService) applicationContext.getBean("tstService");
//调用bean的方法
String talk = tstService.say();
在本次测试中:
- TstService是一个接口,有一个TestServiceImpl被xml所注册,id为"testService"
- TestServiceImpl 内包含BookService属性,使用xml将一个BookService注入到该属性中
- TestServiceImpl 方法 say ,调用 bookService 中的一个getPrice方法
即本次过程中
6.3 BeanFactory创建的代码追踪
6.3.1 FileSystemXmlApplicationContext构造器
- 跟踪主代码 ,到FileSystemXmlApplicationContext的构造器
//调用的方法,实际调用的是下方的构造器
public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
this(new String[]{configLocation}, true, (ApplicationContext)null);
}
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
super(parent);
this.setConfigLocations(configLocations);
if (refresh) {
this.refresh();
}
}
- super(parent) 通过多次跟踪,其最终的执行是AbstractApplicationContext类中的构造,主要是设置了parent,这里设置的是null,我们可以理解为初始化了父容器
- this.setConfigLocations(configLocations); 设置资源文件的位置
- refresh() 核心方法,这个方法是在AbstractApplicationContext中定义的模板方法,
6.3.2 refresh()方法具体分析
refresh()方法定义在ConfigurableApplicationContext接口中 如下所示:
void refresh() throws BeansException, IllegalStateException;
AbstractApplicationContext中,重写了这个方法,定义了一系列的方法链,即步骤,它并没有实现了所有的逻辑,只是充当了一个模板,由其子类去实现更多个性化的逻辑 代码如下:
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//做准备
prepareRefresh();
//创建beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//为此上下文准备bean工厂
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// 实例化所有剩余的(非lazy-init)单例。
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);
}
// 销毁已创建的单例
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// 异常向上抛出
throw ex;
}
finally {
// 重置公共缓存
resetCommonCaches();
}
}
}
在refresh方法中 最核心的两部如下
- 1.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
- 2.finishBeanFactoryInitialization(beanFactory);
6.3.2 obtainFreshBeanFactory()与finishBeanFactoryInitialization(beanFactory)方法跟踪
分析obtainFreshBeanFactory() 创建BeanFactory
AbstractApplicationContext中对该方法进行了实现,其代码如下:
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();//刷新BeanFactory
return getBeanFactory();//获取BeanFactory--实际上这个BeanFactory是上一步创建的,
}
refreshBeanFactory()具体实现是由子类AbstractRefreshableApplicationContext 实现的,其实现逻辑 1.检查上下文是否存在BeanFactory,如果已存在则先进行销毁
2.创建一个空的BeanFactory 3.加载bean
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {//如果存在,则先销毁bean
destroyBeans();
closeBeanFactory();
}
try {
//创建一个空的BeanFactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
//将context的id设置为beanFactory的SerializationId。
beanFactory.setSerializationId(getId());
//定义bean,对beanFactory做了一些设置 ,在当前情况下,其实现AbstractRefreshableApplicationContext中包含两个方法 setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);和beanFactory.setAllowCircularReferences(this.allowCircularReferences);
customizeBeanFactory(beanFactory);
//加载BeanDefinitions
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
接下来进一步分析loadBeanDefinitions,该方法定义在AbstractRefreshableApplicationContext类中,当前环境下具体实现在AbstractXmlApplicationContext中其实现如下:
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// 创建XmlBeanDefinitionReader
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// 使用当前环境配置beanDefinitionReader
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// 允许子类提供自定义的对beanDefinitionReader的初始化
//此环境中主要是使用了以下两个方法
//1. beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverri ing);
//2. beanFactory.setAllowCircularReferences(this.allowCircularReferences);
initBeanDefinitionReader(beanDefinitionReader);
//通过beanDefinitionReader加载BeanDefinitions
loadBeanDefinitions(beanDefinitionReader);
}
可以看到上面方法体主要是围绕XmlBeanDefinitionReader来进行的,通过beanDefinitionReader这个对象来对传入的beanfactory进行处理,方法体的前部分都是对beanDefinitionReader这个对象本身的一些配置,直到最后一步。
最后一步依然是loadBeanDefinitions()方法,该方法是一个重载方法其代码如下:
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
这个方法主要是 :解析资源文件的位置,然后调用XmlBeanDefinitionReader 解析Bean的定义,此时getConfigResources()方法返回的为null,接下来主要看 reader.loadBeanDefinitions(configLocations)
这个方法,这里插入一张XmlBeanDefinitionReader 的类图
该方法实现是在XmlBeanDefinitionReader的父类AbstractBeanDefinitionReader中,其源码如下:
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
Assert.notNull(locations, "Location array must not be null");
int count = 0;
for (String location : locations) {
count += loadBeanDefinitions(location);
}
return count;
}
其接受的是一多个String loction,它会遍历所有资源数组 我们上面只定义了一个xml即classpath:spring-chapter2.xml
,所以这里的locations只有一个,其内容为classpath:spring-chapter2.xml
其核心代码是loadBeanDefinitions();
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
return loadBeanDefinitions(location, null);
}
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");
}
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
//这里是主要做跟踪的
int count = loadBeanDefinitions(resources);
if (actualResources != null) {
Collections.addAll(actualResources, resources);
}
if (logger.isTraceEnabled()) {
logger.trace("Loaded " + count + " bean definitions from location pattern [" + location + "]");
}
return count;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
Resource resource = resourceLoader.getResource(location);
int count = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isTraceEnabled()) {
logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
}
return count;
}
}
上方这个方法解析资源文件的路径,得到Resource[]资源数组,核心逻辑是int count = loadBeanDefinitions(resources);
这个方法定义在 BeanDefinitionReader中 ,在AbstractBeanDefinitionReader中实现, 其内部代码如下:
@Override
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
Assert.notNull(resources, "Resource array must not be null");
int count = 0;
for (Resource resource : resources) {
count += loadBeanDefinitions(resource);
}
return count;
}
就是遍历resourcs,并调用loadBeanDefinitions,该方法定义在BeanDefinitionReader中具体实现在XmlBeanDefinitionReader中。
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
其调用了自己方法的重载:
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Loading XML bean definitions from " + encodedResource);
}
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try (InputStream inputStream = encodedResource.getResource().getInputStream()) {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
该方法以流的形式读取资源文件,其中doLoadBeanDefinitions是载入Bean定义的核心方法,其代码如下
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
int count = registerBeanDefinitions(doc, resource);
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + count + " bean definitions from " + resource);
}
return count;
}.....catch{}//忽略n个catch代码
}
从上方代码可以看出,最终注册Bean的地方是在registerBeanDefinitions(doc,resource);其代码如下
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getRegistry().getBeanDefinitionCount();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}
在以上的代码中,核心是documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
这里发生了对Bean的注册,该方法定义在BeanDefinitionDocumentReader中 其方法实现如下
@Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
doRegisterBeanDefinitions(doc.getDocumentElement());
}
继续跟入到doRegisterBeanDefinitions(doc.getDocumentElement());
代码详情如下
protected void doRegisterBeanDefinitions(Element root) {
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
if (this.delegate.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
// We cannot use Profiles.of(...) since profile expressions are not supported
// in XML config. See SPR-12458 for details.
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
if (logger.isDebugEnabled()) {
logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec +
"] not matching: " + getReaderContext().getResource());
}
return;
}
}
}
preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);
this.delegate = parent;
}
继续跟进到核心代码 parseBeanDefinitions(root, this.delegate);
中,其实现如下
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
核心方法是parseDefaultElement(ele, delegate);
查看其代码如下:
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}
当前环境下,我们进入的是processBeanDefinition(ele, delegate);
其实现如下:
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
从以上方法代码可知,最关键的是BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
这是注册bean的关键代码:查看其代码如下:
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());这一行将Bean的名字和BeanDefinition对象进行注册的地方,它是在BeanDefinitionRegistry中定义的,当前环境下,其实现类是DefaultListableBeanFactory中,其代码如下:
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
if (existingDefinition != null) {
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
}
else if (existingDefinition.getRole() < beanDefinition.getRole()) {
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if (logger.isInfoEnabled()) {
logger.info("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
existingDefinition + "] with [" + beanDefinition + "]");
}
}
else if (!beanDefinition.equals(existingDefinition)) {
if (logger.isDebugEnabled()) {
logger.debug("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
this.beanDefinitionMap.put(beanName, beanDefinition);
}
else {
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
this.beanDefinitionMap.put(beanName, beanDefinition);
List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
removeManualSingletonName(beanName);
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
removeManualSingletonName(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (existingDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
else if (isConfigurationFrozen()) {
clearByTypeCache();
}
}
以上代码,先从beanDefinitionMap这个ConcurrentHashMap对象根据beanName查找查找是否已经有同名的bean,如果不存在,则会调用beanDefinitionMap.put(beanName, beanDefinition);以beanName为key,beanDefinition为value注册,将这个bean注册到BeanFactory中,并将所有的BeanName保存到beanDefinitionNames这个ArrayList中。
到此,完成了IOC第一部分,创建BeanFactory的代码解析。但是此时只完成了bean名称和BeanDefinition对象的注册,并没有实现bean的实例化和依赖注入。下面我们继续分析Bean的初始化。
6.4 实例化Bean的代码追踪
在上面BeanFactory创建过程中,BeanDefinition注册到了BeanFactory中的一个ConcurrentHashMap对象中,并且以BeanName为key,BeanDefinition为value注册。
下面从AbstractApplicationContext类的refresh()方法中的finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory)开始向下分析
进入到该方法内部,其代码如下:
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// 为上下文初始化转换服务
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no bean post-processor
//如果没有bean后置处理器,则注册一个内嵌值解释器
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// 主要用于解析注释属性值。
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
从上面代码中,我们可以看到,beanFactory.preInstantiateSingletons();
这行代码是实例化Bean的,该方法定义在ConfigurableListableBeanFactory中,具体实现在 DefaultListableBeanFactory中,跟踪进入该方法,代码如下:
@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("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<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) ((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...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
该方法遍历beanDefinitionNames(是一个ArrayList)中的BeanName,调用getBean(beanName)方法,这个方法底层创建了bean,并递归构建bean之间的依赖关系。getBean(beanName)方法最终会调用doGetBean(name,null,null,false);其代码较长,这里看主要的部分
.......
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance.
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
..........
在方法中,首先会获取当前Bean依赖关系mbd.getDependsOn();接着根据依赖的BeanName递归调用getBean()方法,直到调用getSingleton()方法返回依赖Bean结束。getSingleton()方法的参数是createBean()方法返回值。createBean()是在AbstractBeanFactory中定义的,在AbstractAutowireCapableBeanFactory中实现了该方法,其内容如下
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
// Make sure bean class is actually resolved at this point, and
// clone the bean definition in case of a dynamically resolved Class
// which cannot be stored in the shared merged bean definition.
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
// Prepare method overrides.
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
try {
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// A previously detected exception with proper bean creation context already,
// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}
以上,doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)这个方法是核心方法,其主要相关代码如下:
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
........
// Initialize the bean instance.
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
.........
return exposedObject;
}
以上方法代码中:
- 1.
instanceWrapper=createBeanInstance(beanName,mbd,args)
用来创建实例 - 2.
populateBean(beanName,mbd,instanceWrapper)
用于填充Bean,即依赖注入的核心代码
先看createBeanInstance
这个方法,定义在AbstractAutowireCapableBeanFactory
中,其核心代码如下
.......
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
.......
这个方法会调用instantiateBean(beanName, mbd)
,进入该方法其部分实现(类AbstractAutowireCapableBeanFactory)如下:
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
try {
Object beanInstance;
if (System.getSecurityManager() != null) {
beanInstance = AccessController.doPrivileged(
(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
getAccessControlContext());
}
else {
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
}
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
}
}
核心代码 beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
即使用InstantiationStrategy对象的静态方法instantiate,instantiate方法主要调用了BeanUtils.instantiateClass(constructorToUse);
这个方法,该方法部分实现如下:
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor);
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
return KotlinDelegate.instantiateClass(ctor, args);
}
else {
Class<?>[] parameterTypes = ctor.getParameterTypes();
Assert.isTrue(args.length <= parameterTypes.length, "Can't specify more arguments than constructor parameters");
Object[] argsWithDefaultValues = new Object[args.length];
for (int i = 0 ; i < args.length; i++) {
if (args[i] == null) {
Class<?> parameterType = parameterTypes[i];
argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
}
else {
argsWithDefaultValues[i] = args[i];
}
}
return ctor.newInstance(argsWithDefaultValues);
}
}
.......
}
该方法会判断是否是kotlin类型,如果不是Kotlin类型,则调用Constructor的newInstance方法,也就是最终使用反射创建了该实例。到这里Bean实例已经创建完成,但是依赖关系还没有设置,先在回到doCreateBean()方法中populatedBean(beanName,mbd,instanceWrapper)方法,该方法用于填充Bean,即依赖注入的实现,下面进入到AbstractAutowireCapableBeanFactory类中的populateBean()方法中,其核心代码如下:
.......
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
.......
if (pvs != null) {
applyPropertyValues(beanName, mbd, bw, pvs);
}
方法的主要逻辑是PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
即获取当前bean的所有属性,就是配置property元素,最后执行applyPropertyValues(beanName, mbd, bw, pvs);
其方法在AbstractAutowireCapableBeanFactory中关键内容如下:
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
.......
for (PropertyValue pv : original) {
if (pv.isConverted()) {
deepCopy.add(pv);
}
else {
String propertyName = pv.getName();
Object originalValue = pv.getValue();
.......
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
.........
bw.setPropertyValues(new MutablePropertyValues(deepCopy));
........
}
其中Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
用来获取property对应的值,BeanDefinitionValueResolver中定义的valueResolver.resolveValueIfNecessary(pv, originalValue);
方法的部分代码如下:
// We must check each value to see whether it requires a runtime reference
// to another bean to be resolved.
if (value instanceof RuntimeBeanReference) {
RuntimeBeanReference ref = (RuntimeBeanReference) value;
return resolveReference(argName, ref);
}
.........
核心方法resolveReference(argName, ref);
进入该方法查看其代码如下:
@Nullable
private Object resolveReference(Object argName, RuntimeBeanReference ref) {
try {
Object bean;
Class<?> beanType = ref.getBeanType();
if (ref.isToParent()) {
BeanFactory parent = this.beanFactory.getParentBeanFactory();
if (parent == null) {
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Cannot resolve reference to bean " + ref +
" in parent factory: no parent factory available");
}
if (beanType != null) {
bean = parent.getBean(beanType);
}
else {
bean = parent.getBean(String.valueOf(doEvaluate(ref.getBeanName())));
}
}
else {
String resolvedName;
if (beanType != null) {
NamedBeanHolder<?> namedBean = this.beanFactory.resolveNamedBean(beanType);
bean = namedBean.getBeanInstance();
resolvedName = namedBean.getBeanName();
}
else {
resolvedName = String.valueOf(doEvaluate(ref.getBeanName()));
bean = this.beanFactory.getBean(resolvedName);
}
this.beanFactory.registerDependentBean(resolvedName, this.beanName);
}
if (bean instanceof NullBean) {
bean = null;
}
return bean;
}
catch (BeansException ex) {
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
}
}
核心代码bean = parent.getBean(String.valueOf(doEvaluate(ref.getBeanName())));
,
这里将发生递归调用,根据依赖的名称,从BeanFactory中递归得到依赖。这样就可以获得依赖,往前推到applyPropertyValues入口处,拿到依赖后,调用bw.setPropertyValues(new MutablePropertyValues(deepCopy));
这是将依赖注入的地方,进入该方法:其实际上调用的是public void setPropertyValues(PropertyValues pvs, false, false)
该方法在AbstractPropertyAccessor这个类中,代码如下
public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid)
throws BeansException {
List<PropertyAccessException> propertyAccessExceptions = null;
List<PropertyValue> propertyValues = (pvs instanceof MutablePropertyValues ?
((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues()));
if (ignoreUnknown) {
this.suppressNotWritablePropertyException = true;
}
try {
for (PropertyValue pv : propertyValues) {
// setPropertyValue may throw any BeansException, which won't be caught
// here, if there is a critical failure such as no matching field.
// We can attempt to deal only with less serious exceptions.
try {
setPropertyValue(pv);
}
........
}
}
finally {
if (ignoreUnknown) {
this.suppressNotWritablePropertyException = false;
}
}
// If we encountered individual exceptions, throw the composite exception.
if (propertyAccessExceptions != null) {
PropertyAccessException[] paeArray = propertyAccessExceptions.toArray(new PropertyAccessException[0]);
throw new PropertyBatchUpdateException(paeArray);
}
}
该方法会循环Bean的属性列表,并调用setPropertyValue(pv);
,这个方法是被AbstractNestablePropertyAccessor重写,并被我们当前的代码所调用,进入该方法的代码,当前环境下,其代码如下:
@Override
public void setPropertyValue(PropertyValue pv) throws BeansException {
PropertyTokenHolder tokens = (PropertyTokenHolder) pv.resolvedTokens;
if (tokens == null) {
String propertyName = pv.getName();
AbstractNestablePropertyAccessor nestedPa;
try {
nestedPa = getPropertyAccessorForPropertyPath(propertyName);
}
catch (NotReadablePropertyException ex) {
throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName,
"Nested property in path '" + propertyName + "' does not exist", ex);
}
tokens = getPropertyNameTokens(getFinalPath(nestedPa, propertyName));
if (nestedPa == this) {
pv.getOriginalPropertyValue().resolvedTokens = tokens;
}
nestedPa.setPropertyValue(tokens, pv);
}
else {
setPropertyValue(tokens, pv);
}
}
核心是nestedPa.setPropertyValue(tokens, pv);这个方法,进入nestedPa.setPropertyValue(tokens, pv);
个方法,该方法定义在AbstractNestablePropertyAccessor这个当前类中,其代码如下:
protected void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException {
if (tokens.keys != null) {
processKeyedProperty(tokens, pv);
}
else {
processLocalProperty(tokens, pv);
}
}
此时会进入到else代码块执行processLocalProperty(tokens, pv);
这个语句,此方法代码非常复杂,我们主要看核心代码如下:
.........
Object oldValue = null;
PropertyChangeEvent propertyChangeEvent;
try {
Object originalValue = pv.getValue();
Object valueToApply = originalValue;
........
ph.setValue(valueToApply);
........
上述代码中,ph.setValue(valueToApply);
的实现是BeanWrapperImpl这个类,查看其代码如下:
@Override
public void setValue(@Nullable Object value) throws Exception {
Method writeMethod = (this.pd instanceof GenericTypeAwarePropertyDescriptor ?
((GenericTypeAwarePropertyDescriptor) this.pd).getWriteMethodForActualAccess() :
this.pd.getWriteMethod());
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
ReflectionUtils.makeAccessible(writeMethod);
return null;
});
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>)
() -> writeMethod.invoke(getWrappedInstance(), value), acc);
}
catch (PrivilegedActionException ex) {
throw ex.getException();
}
}
else {
ReflectionUtils.makeAccessible(writeMethod);
writeMethod.invoke(getWrappedInstance(), value);
}
}
这里可以看到该方法会找到属性的set方法,然后调用method的invoke方法,完成属性的注入,至此IOC容器的启动过程完毕。
7.总结
Spring IOC的实现主要分为两步:
1.创建BeanFactory
2.实例化Bean
Spring的Bean在内存中的状态就是BeanDefinition,在创建Bean的过程中,需要根据BeanDefinition的信息来递归完成依赖注入,这些递归都是以getBean()为入口的。
其他的后续再补充了!!!