1. 程式人生 > 其它 >Spring筆記(四):依賴注入(DI)

Spring筆記(四):依賴注入(DI)

時間:2021/10/25

下面我們將對不同型別的屬性通過spring的bean容器實現賦值:

1.編寫實體類

Address實體類:

 1 package bupt.machi.pojo;
 2 
 3 public class Address {
 4 
 5     private String city;
 6 
 7     public String getCity() {
 8         return city;
 9     }
10 
11     public void setCity(String city) {
12         this.city = city;
13 } 14 15 @Override 16 public String toString() { 17 return "Address{" + 18 "city='" + city + '\'' + 19 '}'; 20 } 21 }

Student實體類:

 1 package bupt.machi.pojo;
 2 
 3 import java.util.*;
 4 
 5 public class Student {
 6 
 7     private String name;
8 private Address address; 9 private String[] hobbys; 10 11 public void setHobbys(String[] hobbys) { 12 this.hobbys = hobbys; 13 } 14 15 public String[] getHobbys() { 16 return hobbys; 17 } 18 19 private List<String> books; 20 private Map<String, Integer> scores;
21 private Set<String> games; 22 private Properties properties; 23 private String wife; 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public Address getAddress() { 34 return address; 35 } 36 37 public void setAddress(Address address) { 38 this.address = address; 39 } 40 41 public List<String> getBooks() { 42 return books; 43 } 44 45 public void setBooks(List<String> books) { 46 this.books = books; 47 } 48 49 public Map<String, Integer> getScores() { 50 return scores; 51 } 52 53 public void setScores(Map<String, Integer> scores) { 54 this.scores = scores; 55 } 56 57 public Set<String> getGames() { 58 return games; 59 } 60 61 public void setGames(Set<String> games) { 62 this.games = games; 63 } 64 65 public Properties getProperties() { 66 return properties; 67 } 68 69 public void setProperties(Properties properties) { 70 this.properties = properties; 71 } 72 73 public String getWife() { 74 return wife; 75 } 76 77 public void setWife(String wife) { 78 this.wife = wife; 79 } 80 81 @Override 82 public String toString() { 83 return "Student{" + 84 "name='" + name + '\'' + 85 ", address=" + address + 86 ", hobbys=" + Arrays.toString(hobbys) + 87 ", books=" + books + 88 ", scores=" + scores + 89 ", games=" + games + 90 ", properties=" + properties + 91 ", wife='" + wife + '\'' + 92 '}'; 93 } 94 }

2.編寫spring配置檔案

 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="address" class="bupt.machi.pojo.Address">
 7         <property name="city" value="qingdao"/>
 8     </bean>
 9 
10     <bean id="student" class="bupt.machi.pojo.Student">
11         <property name="name" value="machi"/>
12 <!--   引用     -->
13         <property name="address" ref="address"/>
14 <!--   陣列     -->
15         <property name="hobbys">
16             <array>
17                 <value>學習</value>
18                 <value>寫程式碼</value>
19                 <value>睡覺</value>
20             </array>
21         </property>
22 <!--   list     -->
23         <property name="books">
24             <list>
25                 <value>黃金時代</value>
26                 <value>冰與火之歌</value>
27             </list>
28         </property>
29 <!--   map     -->
30         <property name="scores">
31             <map>
32                 <entry key="數學" value="100"/>
33                 <entry key="語文" value="100"/>
34             </map>
35         </property>
36 <!--   set     -->
37         <property name="games">
38             <set>
39                 <value>英雄聯盟</value>
40                 <value>王者榮耀</value>
41             </set>
42         </property>
43 <!--   properties     -->
44         <property name="properties">
45             <props>
46                 <prop key="性別"></prop>
47                 <prop key="愛好"></prop>
48             </props>
49         </property>
50 <!--   null     -->
51         <property name="wife">
52             <null/>
53         </property>
54     </bean>
55 </beans>

3.編寫測試類

 1 import bupt.machi.pojo.Student;
 2 import org.junit.jupiter.api.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class MyTest {
 7 
 8     @Test
 9     public void testStudent(){
10         ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
11 
12         Student student = (Student) classPathXmlApplicationContext.getBean("student");
13         System.out.println(student);
14     }
15 }
努力,向上,自律