ClassPathXmlApplicationContext源码分析
ClassPathXmlApplicationContext源码解读
入口函数
通过ClassPathXmlApplicationContext
类构造方法启动父类AbstractApplicationContext
的函数refresh方法
ClassPathXmlApplicationContext
源码:
package org.springframework.context.support;?import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.lang.Nullable;import org.springframework.util.Assert;?public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext { @Nullable private Resource[] configResources;? public ClassPathXmlApplicationContext() { }? public ClassPathXmlApplicationContext(ApplicationContext parent) { super(parent); }? public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[]{configLocation}, true, (ApplicationContext)null); }? public ClassPathXmlApplicationContext(String... configLocations) throws BeansException { this(configLocations, true, (ApplicationContext)null); }? public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent) throws BeansException { this(configLocations, true, parent); }? public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException { this(configLocations, refresh, (ApplicationContext)null); }? public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { super(parent); this.setConfigLocations(configLocations); if (refresh) { this.refresh(); }? }? public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException { this(new String[]{path}, clazz); }? public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException { this(paths, clazz, (ApplicationContext)null); }? public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, @Nullable ApplicationContext parent) throws BeansException { super(parent); Assert.notNull(paths, "Path array must not be null"); Assert.notNull(clazz, "Class argument must not be null"); this.configResources = new Resource[paths.length];? for(int i = 0; i < paths.length; ++i) { this.configResources[i] = new ClassPathResource(paths[i], clazz); }? this.refresh(); }? @Nullable protected Resource[] getConfigResources() { return this.configResources; }}?
ClassPathXmlApplicationContext(String configLocation)
一般使用这个方法,传入一个string类型的xml文件名,通过xml配置文件指定特定的组件,实例化到bean工厂里,即DefaultSingletonBeanRegistry 下的Map集合里
ClassPathXmlApplicationContext(String... configLocations):
可传入多个xml,装配到spring,bean工厂
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) :
传入String数组
@Test public void TestXmlBean(){ ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:xmlRegBean.xml"); Animal cat =context.getBean("cat", Cat.class); cat.getName(); }? @Test public void TestMultiXml(){ ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:xmlRegBean.xml","classpath:xmlRegBean2.xml"); Animal cat =context.getBean("cat", Cat.class); cat.getName(); System.out.println(cat); Animal cat2 =context.getBean("cat2", Cat.class); cat2.getName(); System.out.println(cat2); } @Test public void TestArrayXml(){ String[] arrXml = {"classpath:xmlRegBean.xml","classpath:xmlRegBean2.xml"}; ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(arrXml,true); Animal cat =context.getBean("cat", Cat.class); cat.getName(); System.out.println(cat); Animal cat2 =context.getBean("cat2", Cat.class); cat2.getName(); System.out.println(cat2); }
相关推荐
abfdada 2020-08-26
凉白开 2020-07-19
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06