1. 容器基本用法
public class TestBean {
private String strA = "strA";
public String getStrA() {
return strA;
}
}
public class TestBeanFactory {
@Test
public void test(){
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("TestBeanFactory.xml"));
TestBean bean = (TestBean) beanFactory.getBean("testBean");
Assert.assertEquals("strB", bean.getStrA());
}
}
<?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="testBean" class="com.qmy.test.TestBean"/>
</beans>
2. 功能分析
(1) 读取配置文件TestBeanFactory.xml。
(2) 根据配置文件TestBeanFactory.xml中的配置找到对应的类的配置,并实例化。
(3)调用实例化后的实例。
- ReflectionUtil -> App <- ConfigReader
- ConfigReader:用于读取及验证配置文件。我们要用配置文件里面的东西,需要先读取,然后放到内存中。
- ReflectionUtil:用于根据配置文件中的配置进行反射实例化。
- App:用于完成整个逻辑的串联。
3. 工程搭建
spring-core、spring-beans。