依赖注入有两种方式 通过 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 26 9 14 15 55 5616 17 18 19 2021 26 2728 3429 33set1 30 31set2 3235 43 4445 5446 5347 prop148 4950 prop251 5257
还有一种是通过构造函数 注入
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 26 9 10 14 1511 12 13 16
测试
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 @Test10 public void testSet(){11 Person person = (Person)context.getBean("person_Con");12 System.out.println(person.getPname());13 person.getStudent().student();14 }15 }