Spring学习-02day【bean配置+依赖注入的方式】

Spring学习-02day【bean配置+依赖注入的方式】

 

 Spring学习-02day【bean配置+依赖注入的方式】

 

 地址类

 1 package com.sen.pojo;
 2 
 3 public class Address {
 4     //作为一个引用对象
 5     private String address;
 6 
 7     public String getAddress() {
 8         return address;
 9     }
10 
11     public void setAddress(String address) {
12         this.address = address;
13     }
14 }

学生类

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

Spring学习-02day【bean配置+依赖注入的方式】

 

 (一个空空荡荡的小beans~

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   
8 </beans>

Spring学习-02day【bean配置+依赖注入的方式】

 

 在bean.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     <bean id="address" class="com.sen.pojo.Address">
 8         <property name="address" value="河南"></property>
 9     </bean>
10 
11     <bean id="student" class="com.sen.pojo.Student">
12         <!--普通注入:直接使用value -->
13             <property name="name" value="夜风"/>
14          <!--bean注入:使用ref -->
15             <property name="address" ref="address"/>
16         <!--数组注入:-->
17         <property name="books">
18             <array>
19                 <value>红楼梦</value>
20                 <value>西游记</value>
21                 <value>水浒传</value>
22                 <value>三国演义</value>
23             </array>
24         </property>
25 
26         <!--List集合注入-->
27         <property name="hobbys">
28                 <list>
29                     <value>打王者</value>
30                     <value>听歌</value>
31                     <value>看电影</value>
32                 </list>
33         </property>
34 
35         <!--Map集合注入:-->
36         <property name="card">
37             <map>
38                 <entry key="身份证" value="411481199711185413"></entry>
39                 <entry key="银行卡" value="834672384732487347"></entry>
40             </map>
41         </property>
42 
43         <!--Set集合注入:-->
44         <property name="games">
45             <set>
46                 <value>LOL</value>
47                 <value>COCO</value>
48                 <value>JOJO</value>
49             </set>
50         </property>
51 
52         <!--null空值注入:-->
53         <property name="wife">
54             <null></null>
55         </property>
56         
57         <!--properties注入:-->
58         <property name="info">
59             <props>
60                 <prop key="学号">202105520034</prop>
61                 <prop key="性别">男</prop>
62                 <prop key="家乡">合肥</prop>
63 
64             </props>
65         </property>
66 
67     </bean>
68 </beans>

最后的结果

Spring学习-02day【bean配置+依赖注入的方式】

 

上一篇:php微信分享demo


下一篇:微信多开管理系统