参考视频:https://www.bilibili.com/video/BV1Bq4y1Q7GZ?p=4
通过视频的学习和自身的理解整理出的笔记。
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.7.RELEASE</version> </dependency> </dependencies>
简单的User类,在测试过程中创建这个User类的对象。
public class User { private Integer id; private String name; public User() { System.out.println("创建了"); } }
在applicationContext.xml配置bean对象。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.zqc.domain.User" id="user"> </bean> </beans>
通过applicationContext.xml配置应用程序的上下文,在容器中创建User对象。
public class SpringDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); } }
目标:BeanDefinition是什么?是什么时候创建的?
前面在分析Bean创建的过程中,发现在执行完refresh()方法后就完成了bean对象的创建。
在测试代码中创建context对象:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ClassPathXmlApplicationContext构造器中调用了另一个构造器:
该构造器中执行了refresh()方法
在refresh()方法中创建了非懒加载的单例对象:
所以BeanDefinition可定在这行代码之前创建的。下面看看在refresh()方法的什么地方创建了BeanDefinition。