Spring的依赖注入不仅可以注入基本类型,也可以注入包括model,list等等类型
package com.hongcong.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hongcong.model.People; public class test { public static void main(String[] args) { ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("beans.xml"); //Bean注入 People people1 = (People)ca.getBean("people1"); System.out.println(people1); //内部bean People people2 = (People)ca.getBean("people2"); System.out.println(people2); //null注入 People people3 = (People)ca.getBean("people3"); System.out.println(people3); //集合注入 People people4 = (People)ca.getBean("people4"); System.out.println(people4); } }
beans.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="dog" class="com.hongcong.model.Dog"> <property name="name" value="Jack"/> </bean> <!--Bean注入 --> <bean id="people1" class="com.hongcong.model.People"> <property name="id" value="1"/> <property name="name" value="小一"/> <property name="age" value="11"/> <property name="dog" ref="dog"></property> </bean> <!--内部bean --> <bean id="people2" class="com.hongcong.model.People"> <property name="id" value="1"/> <property name="name" value="小一"/> <property name="age" value="11"/> <property name="dog"> <bean class="com.hongcong.model.Dog" > <property name="name" value="Tom"></property> </bean> </property> </bean> <!--null注入 --> <bean id="people3" class="com.hongcong.model.People"> <property name="id" value="1"/> <property name="name" value="小一"/> <property name="age" value="11"/> <property name="dog" > <null></null> </property> </bean> <!--list注入 --> <bean id="people4" class="com.hongcong.model.People"> <property name="id" value="1"/> <property name="name" value="小一"/> <property name="age" value="11"/> <property name="loves" > <list> <value>唱歌</value> <value>听歌</value> </list> </property> </bean> </beans>
people
package com.hongcong.model; import java.util.ArrayList; import java.util.List; public class People { private int id; private String name; private int age; private Dog dog; List<String> loves = new ArrayList<String>(); public People() { super(); // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public List<String> getLoves() { return loves; } public void setLoves(List<String> loves) { this.loves = loves; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + ", loves=" + loves + "]"; } }
dog
package com.hongcong.model; public class Dog { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
时间: 2024-11-05 13:35:24