//赋值注入
<?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">
<bean id="school" class="ioc.iocSetter.School">
<property name="id">
<value>10006</value>
</property>
<property name="name">
<value>BUAA</value>
</property>
</bean>
<bean id="student" class="ioc.iocSetter.Student">
<property name="id">
<value>101</value>
</property>
<property name="name">
<value>Bill</value>
</property>
<property name="school">
<ref bean="school"/>
</property>
<property name="courses">
<list>
<value>英语</value>
<value>物理</value>
<value>高等数学</value>
<value>软件工程</value>
</list>
</property>
</bean>
</beans>
//测试
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class GreetingMain {
public static void main(String[] args) {
//加载配置文件
ClassPathResource resource = new ClassPathResource("ioc/iocSample/beans.xml");
//根据配置文件构造BeanFactory对象
BeanFactory factory = new XmlBeanFactory(resource);
//取得提供具体业务逻辑的Java Bean
Action action = (Action)factory.getBean("greet");
//调用Java Bean中的具体方法
action.greet();
}
}
//构造器注入
<?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">
<bean id="school" class="ioc.iocConstructor.School">
<property name="id">
<value>10006</value>
</property>
<property name="name">
<value>BUAA</value>
</property>
</bean>
<bean id="student" class="ioc.iocConstructor.Student">
<constructor-arg index="0">
<value>101</value>
</constructor-arg>
<constructor-arg index="1">
<value>Bill</value>
</constructor-arg>
<constructor-arg index="2">
<list>
<value>英语</value>
<value>物理</value>
<value>高等数学</value>
<value>软件工程</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<ref bean="school"/>
</constructor-arg>
</bean>
</beans>