Spring(2):依赖注入DI

 依赖注入DI

  • 当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入

  • 所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理

 

例题

1,建实体类Address和Student

Student

复制代码
 1 public class Student {
 2     private String name;
 3     private Address address;
 4     private String[] books;
 5     private List<String> hobbys;
 6     private Map<String,String> card;
 7     private Set<String> games;
 8     private String girlFriend;
 9     private Properties info;
10 
11     public Student() {
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 
18     public void setAddress(Address address) {
19         this.address = address;
20     }
21 
22     public void setBooks(String[] books) {
23         this.books = books;
24     }
25 
26     public void setHobbys(List<String> hobbys) {
27         this.hobbys = hobbys;
28     }
29 
30     public void setCard(Map<String, String> card) {
31         this.card = card;
32     }
33 
34     public void setGames(Set<String> games) {
35         this.games = games;
36     }
37 
38     public void setGirlFriend(String girlFriend) {
39         this.girlFriend = girlFriend;
40     }
41 
42     public void setInfo(Properties info) {
43         this.info = info;
44     }
45 
46     @Override
47     public String toString() {
48         return "Student{" +
49                 "name='" + name + '\'' +
50                 ", address=" + address.toString() +
51                 ", books=" + Arrays.toString(books) +
52                 ", hobbys=" + hobbys +
53                 ", card=" + card +
54                 ", games=" + games +
55                 ", girlFriend='" + girlFriend + '\'' +
56                 ", info=" + info +
57                 '}';
58     }
59 }
复制代码

 

Address

复制代码
 1 public class Address {
 2     private String address;
 3 
 4     public Address() {
 5     }
 6 
 7     public void setAddress(String address) {
 8         this.address = address;
 9     }
10 
11     @Override
12     public String toString() {
13         return "Address{" +
14                 "address='" + address + '\'' +
15                 '}';
16     }
17 }
复制代码

2,建配置文件 beans.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.xsd">
 6 
 7     <!--Address-->
 8     <bean id="addr" class="pojo.Address">
 9         <property name="address" value="西安"/>
10     </bean>
11 
12     <!--Student-->
13     <bean id="Student" class="pojo.Student">
14 
15         <!--常量注入 普通字段-->
16         <property name="name" value="闪电侠"/>
17 
18         <!--Bean注入 引用其他bean使用ref-->
19         <property name="address" ref="addr"/>
20 
21         <!--数组注入-->
22         <property name="books">
23             <array>
24                 <value>西游记</value>
25                 <value>水浒传</value>
26                 <value>红楼梦</value>
27                 <value>三国演义</value>
28             </array>
29         </property>
30 
31         <!--List注入-->
32         <property name="hobbys">
33             <list>
34                 <value>代码</value>
35                 <value>电影</value>
36                 <value>音乐</value>
37             </list>
38         </property>
39 
40         <!--Map注入-->
41         <property name="card">
42             <map>
43                 <entry key="IdCard" value="666666888888884444"/>
44                 <entry key="银行卡" value="111122223333444"/>
45             </map>
46         </property>
47 
48         <!--Set注入-->
49         <property name="games">
50             <set>
51                 <value>王者荣耀</value>
52                 <value>绝地求生</value>
53             </set>
54         </property>
55 
56         <!--Null空值注入-->
57         <property name="girlFriend">
58             <null/>
59         </property>
60 
61         <!--Properties注入-->
62         <property name="info">
63             <props>
64                 <prop key="学号">2019032324</prop>
65                 <prop key="性别">男</prop>
66                 <prop key="姓名">闪电侠</prop>
67             </props>
68         </property>
69 
70     </bean>
71     
72 </beans>
复制代码

3,编写测试类

复制代码
 1 import org.junit.Test;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 public class StudentTest {
 5     @Test
 6     public void Test(){
 7         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 8         Student student = (Student) context.getBean("Student");
 9         System.out.println(student.toString());
10     }
11 }
复制代码

 

4,运行结果

 

posted @ 2020-07-20 09:47  edda_huang  阅读(148)  评论(0编辑  收藏  举报