spring是一个装配工厂,装好的beans可以拿来用。
<?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-2.0.xsd">
<!-- 创建Student实例 -->
<bean id="student" class="spring.student">
<property name="id" value="1"></property>
<property name="name" value="wsp"></property>
</bean>
<bean id="teacher" parent="student">
<property name="id" value="3"></property>
</bean>
</beans>
beans可继承依赖,调用语句:
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans1.xml");
student stu=(student)ctx.getBean("student");
student tea=(student)ctx.getBean("teacher");
System.out.println(stu.getName()+"\t"+tea.getId());