断点:
spring-ioc.xml
<?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 id="person" class="com.ybb.springmydemo.dto.Person">
</bean>
</beans>
Spring基础容器BeanFactory:
import com.ybb.springmydemo.dto.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@SpringBootTest
class SpringMyDemoApplicationTests {
@Test
void contextLoads() throws Exception {
/** spring基础容器 */
Resource resource = new ClassPathResource("spring/spring-ioc.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
/** BeanDefinition注册流程的入口 */
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
reader.loadBeanDefinitions(resource);
/** getBean的时候才会创建Bean实例对象 */
Person person = (Person) beanFactory.getBean("person");
System.out.println(person);
}
}
Spring高级容器ApplicationContext
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SpringBootTest
class SpringMyDemoApplicationTests {
@Test
void contextLoads() throws Exception {
/** 创建IOC容器,并进行初始化, 此时Bean实例已经被创建完成 */
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-ioc.xml");
System.out.println("=================================");
/** 获取Bean的实例,并验证Bean的实例是否是单例模式的 */
Person person = (Person)context.getBean("person");
Person person2 = (Person)context.getBean("person");
System.out.println("person:"+person);
System.out.println("person2:"+person2);
}
}