Spring容器
示例 代码
public class TestBean {
private String testStr = "1024";
public String getTestStr() {
return testStr;
}
public void setTestStr(String testStr) {
this.testStr = testStr;
}
}
class Test1 {
public static void main(String[] args) {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("org/springframework/beans/demo/TestBeanEntity.xml"));
TestBean bean1 = (TestBean) bf.getBean("bean1");
System.out.println(bean1.getTestStr());
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<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
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="bean1" class="org.springframework.tests.demo.TestBean"/>
</beans>
功能分析
- 读取配置文件
TestBeanEntity.xml
- 根据文件中的配置,找到对应的类配置,并实例化
- 调用实例化的实例