Loading

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 }

 

4.总结

依赖注入的方式:

  • 构造器注入(通过有参构造方法给属性赋值)
  • Set方式注入
  • 拓展方式注入(通过p命名空间和c命名空间,不能直接使用,需要导入xml约束)

 

posted @ 2021-10-25 15:21    阅读(33)  评论(0编辑  收藏  举报