spring启动类ClassPathXmlApplicationContext,读取xml文件并且创建bean
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)throws BeansException {super(parent);setConfigLocations(configLocations);if (refresh) {refresh();}}
第一步.
01.其中的父类构造方法:
super(parent);
02
ClassPathXmlApplicationContext 继承 AbstractXmlApplicationContext:
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext
AbstractXmlApplicationContext的构造方法:
public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {super(parent);}
03.AbstractXmlApplicationContext 继承 AbstractRefreshableConfigApplicationContext
AbstractRefreshableConfigApplicationContext的构造方法:
public AbstractRefreshableConfigApplicationContext(@Nullable ApplicationContext parent) {super(parent);}
04.AbstractRefreshableConfigApplicationContext 继承 AbstractRefreshableApplicationContext
AbstractRefreshableApplicationContext的构造方法:
public AbstractRefreshableApplicationContext(@Nullable ApplicationContext parent) {super(parent);}
05.AbstractRefreshableApplicationContext 继承 AbstractApplicationContext
AbstractApplicationContext的构造方法:
public AbstractApplicationContext(@Nullable ApplicationContext parent) {this();setParent(parent);}public AbstractApplicationContext() {this.resourcePatternResolver = getResourcePatternResolver();}
getResourcePatternResolver() 解析资源的函数,比如xml文件
protected ResourcePatternResolver getResourcePatternResolver() {return new PathMatchingResourcePatternResolver(this);}
06.AbstractApplicationContext的相关属性
/** Logger used by this class. Available to subclasses. *///日志protected final Log logger = LogFactory.getLog(getClass());/** Unique id for this context, if any. *///唯一标识符private String id = ObjectUtils.identityToString(this);/** Display name. */private String displayName = ObjectUtils.identityToString(this);/** Parent context. */@Nullableprivate ApplicationContext parent;/** Environment used by this context. */@Nullableprivate ConfigurableEnvironment environment;/** BeanFactoryPostProcessors to apply on refresh. *///BeanFactoryPostProcessor的集合private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();/** System time in milliseconds when this context started. */private long startupDate;/** Flag that indicates whether this context is currently active. */private final AtomicBoolean active = new AtomicBoolean();/** Flag that indicates whether this context has been closed already. */private final AtomicBoolean closed = new AtomicBoolean();/** Synchronization monitor for the "refresh" and "destroy". */private final Object startupShutdownMonitor = new Object();@Nullableprivate Thread shutdownHook;/** ResourcePatternResolver used by this context. */private final ResourcePatternResolver resourcePatternResolver;/** LifecycleProcessor for managing the lifecycle of beans within this context. */@Nullableprivate LifecycleProcessor lifecycleProcessor;/** MessageSource we delegate our implementation of this interface to. */@Nullableprivate MessageSource messageSource;
第二步.setConfigLocations(configLocations) 设置配置文件的路径