Spring 加载多个xml配置文件的原理分析

网友投稿 626 2023-01-06

Spring 加载多个xml配置文件的原理分析

Spring 加载多个xml配置文件的原理分析

目录示例spring-configlication.xml:spring-config-instance-factory.xmljava示例代码实现AbstractRefreshableConfigApplicationContextAbstractApplicationContextAbstractRefreshableApplicationContextAbstractXmlApplicationContext

示例

先给出两个Bean的配置文件:

spring-configlication.xml:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

spring-config-instance-factory.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

java示例代码

public class ConfigLocationsDemo {

public static void main(String[] args) {

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();

applicationContext.setConfigLocations("spring-configlocation.xml","spring-config-instance-factory.xml");

applicationContext.refresh();

String[] beanNames = applicationContext.getBeanDefinitionNames();

for (String beanName : beanNames) {

System.out.println(beanName);

}

}

}

这样我们就会在控制台打印出两个配置文件中所有的Bean.

person

ChineseFemaleSinger

carFactory

instanceCar

brand

Process finished with exit code 0

实现

AbstractRefreshableConfigApplicationContext

从类的名字推导出这是一个带刷新功能并且带配置功能的应用上下文。

/**

* Set the config locations for this application context.

*

If not set, the implementation may use a default as appropriate.

*/

public void setConfigLocations(@Nullable String... locations) {

if (locations != null) {

this.configLocations = new String[locations.length];

for (int i = 0; i < locationsdzWTL.length; i++) {

this.configLocations[i] = resolvePath(locations[i]).trim();

}

}

else {

this.configLocations = null;

}

}

这个方法很好理解,首先根据传入配置文件路径字符串数组遍历,并且里面调用了resolvePath方法解析占位符。那么我们要想下了,这里只做了解析占位符并且把字符串赋值给configLocations变量,那必然肯定会在什么时候去读取这个路径下的文件并加载bean吧?会不会是在应用上下文调用refresh方法的时候去加载呢?带着思考我们来到了应用上下文的refresh方法。

AbstractApplicationContext

@Override

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

// Tell the subclass to refresh the internal bean factory.

//告诉子类刷新 内部BeanFactory

//https://iteye.com/blog/rkdu2-163-com-2003638

//内部会加载bean定义

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

}

}

/**

* Tell the subclass to refresh the internal bean factory.

* @return the fresh BeanFactory instance

* @see #refreshBeanFactory()

* @see #getBeanFactory()

*/

//得到刷新过的beanFactory

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {

//抽象类AbstractRefreshableApplicationContext

//里面会加载bean定义

//todo 加载bean定义

refreshBeanFactory();

//如果beanFactory为null 会报错

return getBeanFactory();

}

/**

* Subclasses must implement this method to perform the actual configuration load.

* The method is invoked by {@link #refresh()} before any other initialization work.

*

A subclass will either create a new bean factory and hold a reference to it,

* or return a single BeanFactory instance that it holds. In the latter case, it will

* usually throw an IllegalStateException if refreshing the context more than once.

* @throws BeansException if initialization of the bean factory failed

* @throws IllegalStateException if already initialized and multiple refresh

* attempts are not supported

*/

//AbstractRefreshableApplicationContext 实现了此方法

//GenericApplicationContext 实现了此方法

protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

我们看到refreshBeanFactory的第一句注释就提到了Subclasses must implement this method to perform the actual configuration load,意思就是子类必须实现此方法来完成最终的配置加载,那实现此方法的Spring内部默认有两个类,AbstractRefreshableApplicationContext和GenericApplicationContext,这里我们就关心AbstractRefreshableApplicationContext:

AbstractRefreshableApplicationContext

我们要时刻记得上面的AbstractRefreshableConfigApplicationContext类是继承于AbstractRefreshableApplicationContext的,到这里我们给张类关系图以便加深理解:

/**

* This implementation performs an actual refresh of this context's underlying

* bean factory, shutting down the previous bean factory (if any) and

* initializing a fresh bean factory for the next phase of the context's lifecycle.

*/

@Override

protected final void refreshBeanFactory() throws BeansException {

//判断beanFactory 是否为空

if (hasBeanFactory()) {

destroyBeans();

closeBeanFactory(); //设置beanFactory = null

}

try {

DefaultListableBeanFactory beanFactory = createBeanFactory();

//加载Bean定义

//todo AbstractXmlApplicationContext 子类实现 放入beandefinitionMap中

loadBeanDefinitions(beanFactory);

}

catch (IOException ex) {

throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);

}

}

这里就看到了前篇文章提到一个很重要的方法loadBeanDefinitions,我们再拿出来回顾下加深理解:

/**

* Load bean definitions into the given bean factory, typically through

* delegating to one or more bean definition readers.

* @param beanFactory the bean factory to load bean definitions into

* @throws BeansException if parsing of the bean definitions failed

* @throws IOException if loading of bean definition files failed

* @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader

* @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader

*/

protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)

throws BeansException, IOException;

这里因为我们是采用xml配置的,那么肯定是XmlBeanDefinitionReader无疑,我们再回顾下实现此方法的AbstractXmlApplicationContext:

AbstractXmlApplicationContext

/**

* Loads the bean definitions via an XmlBeanDefinitionReader.

* @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader

* @see #initBeanDefinitionReader

* @see #loadBeanDefinitions

*/

//todo 重载了 AbstractRefreshableApplicationContext

@Override

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {

//忽略代码。。

loadBeanDefinitions(beanDefinitionReader);

}

/**

* Load the bean definitions with the given XmlBeanDefinitionReader.

*

The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}

* method; hence this method is just supposed to load and/or register bean definitions.

* @param reader the XmlBeanDefinitionReader to use

* @throws BeansException in case of bean registration errors

* @throws IOException if the required XML document isn't found

* @see #refreshBeanFactory

* @see #getConfigLocations

* @see #getResources

* @see #getResourcePatternResolver

*/

//bean工厂的生命周期由 refreshBeanFactory 方法来处理

//这个方法只是 去加载和注册 bean定义

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {

//ClassPathXmlApplicationContext

Resource[] configResources = getConfigResources();

if (configResources != null) {

reader.loadBeanDefinitions(configResources);

}

String[] configLocations = getConfigLocations();

if (configLocations != null) {

//抽象AbstractBeanDefinitionReader里去加载

reader.loadBeanDefinitions(configLocations);

}

}

这里我们终于到了这个configLocations的用武之地,它就传入了XmlBeanDefinitionReader的loadBeanDefinitions方法中。在这里我们也看到了Spring首先会根据configResources加载BeanDefinition,其次才会去根据configLocations配置去加载BeanDefinition。到这里我们可以学到Spring中对面向对象中封装,继承和多态的运用。下篇文章我们继续剖析Spring关于Xml加载Bean定义的点滴。

以上就是Spring 加载多个xml配置文件的原理分析的详细内容,更多关于Spring 加载xml配置文件的资料请关注我们其它相关文章!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:简单安卓移动应用开发(安卓移动开发软件)
下一篇:小程序生态引流(小程序生态圈)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~