Spring如何加载配置文件

配置文件加载代码调用链

  1. Main方法
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test.xml");
        User user = applicationContext.getBean(User.class);
        System.out.println(user.getName());
    }
  1. ClassPathXmlApplicationContext
	public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {

        // 调用父类的构造方法进行对象的创建以及属性的赋值操作
		super(parent);
		// 将配置文件路径值设置(test.xml)到AbstractRefreshableConfigApplicationContext类的configLocations属性中
		setConfigLocations(configLocations);
		if (refresh) {
		    // BeanDefinitions生成的主要方法
			refresh();
		}
	}
  1. AbstractApplicationContext
@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

			// Prepare this context for refreshing.
			// 准备工作
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			// 创建默认的Bean工厂 根据配置文件生成BeanDefinitions
			**ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();**

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值