SSM-Spring-02:Spring的DI初步加俩个实例

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

 

 


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

 

 

**DI:**依赖注入

 

第一个DEMO:域属性注入

java类:(Car类和Stu类,学生有一辆小汽车)


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
1package cn.dawn.day02di;
2
3/**
4 * Created by Dawn on 2018/3/3.
5 */
6//小汽车类
7public class Car {
8    private String type;
9    private String color;
10
11    public String getType() {
12        return type;
13    }
14
15    public void setType(String type) {
16        this.type = type;
17    }
18
19    public String getColor() {
20        return color;
21    }
22
23    public void setColor(String color) {
24        this.color = color;
25    }
26}
27
28
29
30
31
32
33
34
35package cn.dawn.day02di;
36
37/**
38 * Created by Dawn on 2018/3/3.
39 */
40//学生类
41public class Stu {
42    private String name;
43    private Integer age;
44    private Car car;
45
46    public String getName() {
47        return name;
48    }
49
50    public void setName(String name) {
51        this.name = name;
52    }
53
54    public Integer getAge() {
55        return age;
56    }
57
58    public void setAge(Integer age) {
59        this.age = age;
60    }
61
62    public Car getCar() {
63        return car;
64    }
65
66    public void setCar(Car car) {
67        this.car = car;
68    }
69}
70

配置xml中


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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    <!--汽车-->
7    <bean id="car" class="cn.dawn.day02di.Car">
8        <property name="type" value="奔驰"></property>
9        <property name="color" value="红色"></property>
10    </bean>
11    <!--学生-->
12    <!--这儿的小汽车不能用value,用ref引用上面的那个汽车car-->
13    <bean id="stu" class="cn.dawn.day02di.Stu">
14        <property name="name" value="孟六"></property>
15        <property name="age" value="20"></property>
16        <property name="car" ref="car"></property>
17    </bean>
18
19</beans>
20

测试类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1package cn.dawn.day02;
2
3import cn.dawn.day02di.Stu;
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180303 {
12    @Test
13    /*域属性*/
14    public void t01(){
15        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day02.xml");
16        Stu stu = (Stu) context.getBean("stu");
17        System.out.println(stu.getName()+"开"+stu.getCar().getType());
18    }
19}
20

 

 

 

 

 

第二个Demo:打印机案例

先把架构放上来

SSM-Spring-02:Spring的DI初步加俩个实例

把里面每个类中的代码(code) 放上来


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
1package cn.dawn.day03printer.ink;
2
3/**
4 * Created by Dawn on 2018/3/3.
5 */
6/*墨盒*/
7public interface Ink {
8    public String getInkColor();
9}
10
11
12package cn.dawn.day03printer.ink;
13
14/**
15 * Created by Dawn on 2018/3/3.
16 */
17/*彩色墨盒*/
18public class ColorInk implements Ink {
19    public String getInkColor() {
20        return "彩色墨盒";
21    }
22}
23
24
25
26package cn.dawn.day03printer.ink;
27
28/**
29 * Created by Dawn on 2018/3/3.
30 */
31/*黑白墨盒*/
32public class BlackInk implements Ink {
33    public String getInkColor() {
34        return "黑白墨盒";
35    }
36}
37
38
39
40package cn.dawn.day03printer.paper;
41
42/**
43 * Created by Dawn on 2018/3/3.
44 */
45/*纸张*/
46public interface Paper {
47    public String getPagerSize();
48}
49
50
51
52package cn.dawn.day03printer.paper;
53
54/**
55 * Created by Dawn on 2018/3/3.
56 */
57/*B5纸张*/
58public class B5Paper implements Paper{
59
60    public String getPagerSize() {
61        return "B5纸";
62    }
63}
64
65
66
67package cn.dawn.day03printer.paper;
68
69/**
70 * Created by Dawn on 2018/3/3.
71 */
72/*A4纸张*/
73public class A4Paper implements Paper {
74    public String getPagerSize() {
75        return "A4纸";
76    }
77}
78
79
80
81package cn.dawn.day03printer.printer;
82
83import cn.dawn.day03printer.ink.Ink;
84import cn.dawn.day03printer.paper.Paper;
85
86/**
87 * Created by Dawn on 2018/3/3.
88 */
89/*打印机*/
90public class Printer {
91    /*墨盒*/
92    private Ink ink;
93    /*纸张*/
94    private Paper paper;
95    /*打印方法*/
96    public void print(){
97        System.out.println("我们的喷墨打印机,用"+ink.getInkColor()+"和"+paper.getPagerSize()+"打印出了------》我爱Spring");
98    }
99
100
101    public Ink getInk() {
102        return ink;
103    }
104
105    public void setInk(Ink ink) {
106        this.ink = ink;
107    }
108
109    public Paper getPaper() {
110        return paper;
111    }
112
113    public void setPaper(Paper paper) {
114        this.paper = paper;
115    }
116}
117

 

 

 

配置文件中:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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    <!--墨盒-->
7    <bean id="ink" class="cn.dawn.day03printer.ink.ColorInk"></bean>
8    <!--纸张-->
9    <bean id="paper" class="cn.dawn.day03printer.paper.A4Paper"></bean>
10    <!--打印机-->
11    <bean id="printer" class="cn.dawn.day03printer.printer.Printer">
12        <property name="ink" ref="ink"></property>
13        <property name="paper" ref="paper"></property>
14     </bean>
15</beans>
16

 

 

 

单测方法

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1package cn.dawn.day03;
2
3import cn.dawn.day02di.Stu;
4import cn.dawn.day03printer.printer.Printer;
5import org.junit.Test;
6import org.springframework.context.ApplicationContext;
7import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9/**
10 * Created by Dawn on 2018/3/3.
11 */
12public class test20180303 {
13    @Test
14    /*打印机案例*/
15    public void t01(){
16        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day03.xml");
17        Printer printer = (Printer) context.getBean("printer");
18        printer.print();
19    }
20}
21

 

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

C/C++内存泄漏及检测

2022-1-11 12:36:11

安全技术

Redis启动脚本

2021-8-18 16:36:11

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