博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring Di依赖注入
阅读量:6071 次
发布时间:2019-06-20

本文共 5515 字,大约阅读时间需要 18 分钟。

依赖注入有两种方式 通过 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 
2
6
9
14
15
16
17
18
19
20
21
22
list1
23
24
list2
25
26
27
28
29
set1
30
31
set2
32
33
34
35
36
37
map1
38
39
40
41
42
43
44
45
46
47 prop148
49
50 prop251
52
53
54
55 56
57

 

还有一种是通过构造函数 注入

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 
2
6
9
10
11
12
13
14 15
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 }

 

 

转载于:https://www.cnblogs.com/friends-wf/p/3782811.html

你可能感兴趣的文章
几种常见的I2S数据格式(转)
查看>>
关于消息队列的使用
查看>>
对象的创建与销毁
查看>>
case功能菜单选项
查看>>
HP ILO2 使用详细教程
查看>>
Scout YYF I (概率+矩阵快速幂)
查看>>
Reverse and Compare(DP)
查看>>
1503 猪和回文(DP)
查看>>
345. 反转字符串中的元音字母
查看>>
在浏览器输入URL后发生了什么?
查看>>
高德地图定位之浏览器定位
查看>>
[ HNOI 2005 ] 狡猾的商人
查看>>
神经网络建模的一些感悟;
查看>>
FTPS (FTP over SSL) vs. SFTP (SSH 文件传输协议): 我们如何做出选择
查看>>
steam账号分享工具、迅游账号分享工具说明:
查看>>
c++中冒号(:)和双冒号(::)的用法
查看>>
ServletContext保存访问量
查看>>
IMSI,TMSI的关系
查看>>
JPA + Hibernate + PostgreSQL + Maven基本配置示例
查看>>
vue cordova生成app
查看>>