SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入

释放双眼,带上耳机,听听看~!

 

 


1
2
1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2

 

 

 

DI和IOC相比,DI更偏向于实现

 

DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入

构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错

P命名则有注意的是那个头文件 xmlns  xsi需要你去配置一道,我下面有,你直接copy就可以

 

在实体类中(有俩个实体类,我做了关联关系)

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
1package cn.dawn.day05diup;
2
3/**
4 * Created by Dawn on 2018/3/5.
5 */
6public class Car {
7    private String color;
8    private String type;
9
10    public String getColor() {
11        return color;
12    }
13
14    public void setColor(String color) {
15        this.color = color;
16    }
17
18    public String getType() {
19        return type;
20    }
21
22    public void setType(String type) {
23        this.type = type;
24    }
25}
26
27
28
29
30
31
32
33package cn.dawn.day05diup;
34
35/**
36 * Created by Dawn on 2018/3/5.
37 */
38//student类
39public class Student {
40    private String name;
41    private Integer age;
42    private Car car;
43
44    //带参构造
45    public Student(String name, Integer age, Car car) {
46        this.name = name;
47        this.age = age;
48        this.car = car;
49    }
50
51    //无参构造
52    public Student() {
53    }
54
55    public String getName() {
56        return name;
57    }
58
59    public void setName(String name) {
60        this.name = name;
61    }
62
63    public Integer getAge() {
64        return age;
65    }
66
67    public void setAge(Integer age) {
68        this.age = age;
69    }
70
71    public Car getCar() {
72        return car;
73    }
74
75    public void setCar(Car car) {
76        this.car = car;
77    }
78}
79

 

在大配置xml文件中

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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" xmlns:p="http://www.springframework.org/schema/p"
4       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
5
6    <bean id="car" class="cn.dawn.day05diup.Car">
7        <property name="color" value="黑色"></property>
8        <property name="type" value="奥迪"></property>
9    </bean>
10    <!--di构造注入-->
11    <!--<bean id="student" class="cn.dawn.day05diup.Student">
12        <constructor-arg index="0" value="孟六"></constructor-arg>
13        <constructor-arg index="1" value="20"></constructor-arg>
14        <constructor-arg index="2" ref="car"></constructor-arg>
15    </bean>-->
16
17    <!--p命名注入-->
18    <bean id="student" class="cn.dawn.day05diup.Student" p:name="孟小六" p:age="8" p:car-ref="car"></bean>
19
20</beans>
21

 

没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index

单测方法:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1@Test
2    /*diP命名注入*/
3    public void t02(){
4        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
5        Student student = (Student) context.getBean("student");
6        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
7    }
8
9
10
11    @Test
12    /*di构造注入*/
13    public void t01(){
14        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
15        Student student = (Student) context.getBean("student");
16        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
17    }
18

 

 

集合注入:

数组,List,Set,Map,Properties

实体类

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
1package cn.dawn.day05diup;
2
3import java.util.*;
4
5/**
6 * Created by Dawn on 2018/3/5.
7 */
8public class MyCollection {
9    private String[] array;
10    private List<String> list;
11    private Set<String> set;
12    private Map<String,String> map;
13    private Properties properties;
14
15    @Override
16    public String toString() {
17        return "MyCollection{" +
18                "array=" + Arrays.toString(array) +
19                ", list=" + list +
20                ", set=" + set +
21                ", map=" + map +
22                ", properties=" + properties +
23                '}';
24    }
25
26    public String[] getArray() {
27        return array;
28    }
29
30    public void setArray(String[] array) {
31        this.array = array;
32    }
33
34    public List<String> getList() {
35        return list;
36    }
37
38    public void setList(List<String> list) {
39        this.list = list;
40    }
41
42    public Set<String> getSet() {
43        return set;
44    }
45
46    public void setSet(Set<String> set) {
47        this.set = set;
48    }
49
50    public Map<String, String> getMap() {
51        return map;
52    }
53
54    public void setMap(Map<String, String> map) {
55        this.map = map;
56    }
57
58    public Properties getProperties() {
59        return properties;
60    }
61
62    public void setProperties(Properties properties) {
63        this.properties = properties;
64    }
65}
66

 

大配置中的bean节点

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1<!--di的集合注入-->
2    <bean id="mycollection" class="cn.dawn.day05diup.MyCollection">
3        <!--数组注入-->
4        <property name="array">
5            <array>
6                <value>孟六</value>
7                <value>孟六十六</value>
8                <value>孟六百六十六</value>
9            </array>
10        </property>
11        <!--list集合注入-->
12        <property name="list">
13            <list>
14                <value>奥迪</value>
15                <value>奥小迪</value>
16                <value>奥迪迪</value>
17            </list>
18        </property>
19        <!--set集合注入-->
20        <property name="set">
21            <set>
22                <value>set1</value>
23                <value>set2</value>
24                <value>set3</value>
25            </set>
26        </property>
27        <!--map集合注入-->
28        <property name="map">
29            <map>
30                <entry key="姓名">
31                    <value>孟五</value>
32                </entry>
33                <entry key="年龄">
34                    <value>555</value>
35                </entry>
36            </map>
37        </property>
38        <!--properties-->
39        <property name="properties">
40            <props>
41                <prop key="key1">v1</prop>
42                <prop key="key2">v2</prop>
43                <prop key="key3">v3</prop>
44            </props>
45        </property>
46    </bean>
47

 

单测

 


1
2
3
4
5
6
7
8
1@Test
2    /*di集合注入*/
3    public void t03(){
4        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml");
5        MyCollection mycollection = (MyCollection) context.getBean("mycollection");
6        System.out.println(mycollection);
7    }
8

 

由于重写了toString,可以直接一览无余

 

 

 

 

 

** **

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Ubuntu下安装Mysql 以及mysql-query-browser

2022-1-11 12:36:11

病毒疫情

福建省新型冠状病毒肺炎疫情情况

2020-3-30 8:15:00

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索