spring Di依赖注入
依赖注入有两种方式 通过 get set 方法
Person.java
1 package cn.itcast.spring.sh.di.set; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 public class Person { 9 private Long pid; 10 private String pname; 11 private Student student; 12 13 private Set sets; 14 15 public Long getPid() { 16 return pid; 17 } 18 19 public void setPid(Long pid) { 20 this.pid = pid; 21 } 22 23 public String getPname() { 24 return pname; 25 } 26 27 public void setPname(String pname) { 28 this.pname = pname; 29 } 30 31 public Student getStudent() { 32 return student; 33 } 34 35 public void setStudent(Student student) { 36 this.student = student; 37 } 38 39 public Set getSets() { 40 return sets; 41 } 42 43 public void setSets(Set sets) { 44 this.sets = sets; 45 } 46 47 public List getLists() { 48 return lists; 49 } 50 51 public void setLists(List lists) { 52 this.lists = lists; 53 } 54 55 public Map getMap() { 56 return map; 57 } 58 59 public void setMap(Map map) { 60 this.map = map; 61 } 62 63 public Properties getProperties() { 64 return properties; 65 } 66 67 public void setProperties(Properties properties) { 68 this.properties = properties; 69 } 70 71 private List lists; 72 73 private Map map; 74 75 private Properties properties; 76 }
Student.java
1 package cn.itcast.spring.sh.di.set; 2 3 public class Student { 4 5 }
applicationContext-di-set.xml中
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 <!-- 7 把person和student放入到spring容器中 8 --> 9 <!-- 10 property是用来描述一个类的属性 11 基本类型的封装类、String等需要值的类型用value赋值 12 引用类型用ref赋值 13 --> 14 <bean id="person" class="cn.itcast.spring.sh.di.set.Person"> 15 <property name="pid" value="1"></property> 16 <property name="pname" value="aaa"></property> 17 <property name="student"> 18 <ref bean="student"/> 19 </property> 20 <property name="lists"> 21 <list> 22 <value>list1</value> 23 <ref bean="student"/> 24 <value>list2</value> 25 </list> 26 </property> 27 <property name="sets"> 28 <set> 29 <value>set1</value> 30 <ref bean="student"/> 31 <value>set2</value> 32 </set> 33 </property> 34 <property name="map"> 35 <map> 36 <entry key="m1"> 37 <value>map1</value> 38 </entry> 39 <entry key="m2"> 40 <ref bean="student"/> 41 </entry> 42 </map> 43 </property> 44 <property name="properties"> 45 <props> 46 <prop key="prop1"> 47 prop1 48 </prop> 49 <prop key="prop2"> 50 prop2 51 </prop> 52 </props> 53 </property> 54 </bean> 55 56 <bean id="student" class="cn.itcast.spring.sh.di.set.Student"></bean> 57 </beans>
还有一种是通过构造函数 注入
Person.java
1 package cn.itcast.spring.sh.di.constructor; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 public class Person { 9 private Long pid; 10 private String pname; 11 private Student student; 12 13 private Set sets; 14 15 public Person(String pname,Student student){ 16 this.pname = pname; 17 this.student = student; 18 } 19 20 public Person(Long pid,String pname,Student student){ 21 this.pid = pid; 22 this.pname = pname; 23 this.student = student; 24 } 25 26 public Person(){} 27 28 public Long getPid() { 29 return pid; 30 } 31 32 public void setPid(Long pid) { 33 this.pid = pid; 34 } 35 36 public String getPname() { 37 return pname; 38 } 39 40 public void setPname(String pname) { 41 this.pname = pname; 42 } 43 44 public Student getStudent() { 45 return student; 46 } 47 48 public void setStudent(Student student) { 49 this.student = student; 50 } 51 52 public Set getSets() { 53 return sets; 54 } 55 56 public void setSets(Set sets) { 57 this.sets = sets; 58 } 59 60 public List getLists() { 61 return lists; 62 } 63 64 public void setLists(List lists) { 65 this.lists = lists; 66 } 67 68 public Map getMap() { 69 return map; 70 } 71 72 public void setMap(Map map) { 73 this.map = map; 74 } 75 76 public Properties getProperties() { 77 return properties; 78 } 79 80 public void setProperties(Properties properties) { 81 this.properties = properties; 82 } 83 84 private List lists; 85 86 private Map map; 87 88 private Properties properties; 89 }
Student.java
1 package cn.itcast.spring.sh.di.constructor; 2 3 public class Student { 4 public void student(){ 5 System.out.println("student"); 6 } 7 }
applicationContext-di-constructor.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 <!-- 7 8 --> 9 <bean id="person_Con" class="cn.itcast.spring.sh.di.constructor.Person"> 10 <constructor-arg index="0" type="java.lang.Long" value="1"></constructor-arg> 11 <constructor-arg index="1" value="aaa"></constructor-arg> 12 <constructor-arg index="2" ref="student_Con"></constructor-arg> 13 </bean> 14 15 <bean id="student_Con" class="cn.itcast.spring.sh.di.constructor.Student"></bean> 16 </beans>
测试
1 package cn.itcast.spring.sh.test; 2 3 import org.junit.Test; 4 5 import cn.itcast.spring.sh.di.constructor.Person; 6 import cn.itcast.spring.sh.utils.SpringInit; 7 8 public class DIConTest extends SpringInit{ 9 @Test 10 public void testSet(){ 11 Person person = (Person)context.getBean("person_Con"); 12 System.out.println(person.getPname()); 13 person.getStudent().student(); 14 } 15 }