Spring(2):依賴注入DI

 依賴注入DI

  • 當某個角色(多是一個Java實例,調用者)須要另外一個角色(另外一個Java實例,被調用者)的協助時,在 傳統的程序設計過程當中,一般由調用者來建立被調用者的實例。但在Spring裏,建立被調用者的工做再也不由調用者來完成,所以稱爲控制反轉;建立被調用者 實例的工做一般由Spring容器來完成,而後注入調用者,所以也稱爲依賴注入spring

  • 所謂依賴注入,是指程序運行過程當中,若是須要調用另外一個對象協助時,無須在代碼中建立被調用者,而是依賴於外部的注入。Spring的依賴注入對調用者和被調用者幾乎沒有任何要求,徹底支持對POJO之間依賴關係的管理

 

例題數組

1,建實體類Address和Student

Studentide

 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,運行結果

 

相關文章
相關標籤/搜索