配色: 字号:
JavaWeb Spring依赖注入深入学习
2016-10-14 | 阅:  转:  |  分享 
  
JavaWebSpring依赖注入深入学习

一、依赖注入(DI)

依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setting方法的形式。

1构造器注入

1使用构造器注入

使用xml的注入方式

A.通过参数的顺序

张三56

B.通过参数的类型

56张三

具体实例

假如现在要对一个Person类注入参数,Student是一个另外一个类。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16 publicclassPerson{

privateStringpid;

privateStringname;

privateStudentstudent;

publicPerson(Stringpid,Studentstudent){

this.pid=pid;

this.student=student;

}

publicPerson(Stringpid,Stringname){

this.pid=pid;

this.name=name;

}

} 配置applicationContext.xml,假如不进行参数配置,则报错,找不到相应的构造器。配置了相应的参数,则应在类中声明相应的构造函数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">



















编写测试类DIXMLConstructorTest,进行断点调试,将会发现根据配置的参数,进入的构造函数是Person(Stringpid,Studentstudent)

1

2

3

4

5

6

7

8 publicclassDIXMLConstructorTest{

@Test

publicvoidtest1(){

ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");

Personperson=(Person)context.getBean("person");

}

} 2使用属性setter方法进行注入

使用xml的注入方式:

A.简单Bean的注入简单Bean包括两种类型:包装类型和String

1

2

3

4

5







?

1

2

3

4





1.1装配list集合

1

2

3

4

5

6

7



list1

list2





1.2装配set集合

1

2

3

4

5

6

7



list1

list2





1.3装配map

1

2

3

4

5

6

7

8

9

10





map01





map02





map中的的数值和以及的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的。

1.4装配Properties

1

2

3

4

5

6



prop1

prop2



具体实例

1.创建两个对象Person和Student

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22 packagexgp.spring.demo;

importjava.util.List;

importjava.util.Map;

importjava.util.Properties;

importjava.util.Set;

publicclassPerson{

privateStringpid;

privateStringname;

privateStudentstudent;

privateListlists;

privateSetsets;

privateMapmap;

privatePropertiesproperties;

privateObject[]objects;

publicPerson(){

System.out.println("newperson");

}

//省略getter和setter方法

} 1

2

3

4

5

6

7

8

9

10

11 packagexgp.spring.demo;

publicclassStudent{

publicStudent(){

System.out.println("newstudent");

}

publicvoidsay(){

System.out.println("student");

}

} 配置applicationContext.xml文件

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


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">




init-method="init"

lazy-init="true">











list1

list2











set1

set2













map1



















prop1

prop2









aa

bb









编写测试类DIXMLSetterTest

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 packagexgp.spring.test;

importorg.junit.Test;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.www.visa158.comsupport.ClassPathXmlApplicationContext;

importxgp.spring.demo.Person;

publicclassDIXMLSetterTest{

/

spring容器做的事情:

1、spring容器做了什么?(1)启动spring容器

(2)为person和student两个bean创建对象

(3)解析property的name属性,拼接setter方法,解析property的

value或者ref属性,给setter方法传递参数,利用反射技术给对象赋值。

(4)从spring容器中,把对象提取出来,对象调用方法。

2、spring容器执行顺序是什么?

/

@Test

publicvoidtest1(){

ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");

Personperson=(Person)context.getBean("person");

System.out.println(person.getPid());

System.out.println(person.getName());

System.out.println(person.getLists());

System.out.println(person.getSets());

System.out.println(person.getMap());

System.out.println(person.getObjects().length);

}

}

/1

王五

[list1,list2,xgp.spring.demo.Student@76a9b9c]

[set1,set2,xgp.spring.demo.Student@76a9b9c]

{entry1=map1,entry2=map2}

2/ spring容器的执行顺序

1.都是默认设置





2.设置student(lazy-init=true)





3.设置person(lazy-init=true)





总结可以采用两种方法注入参数,构造器要写对应的构造函数,setter要生成相应的setter方法,并编写默认的构造器。

2.5IOC与DI的意义

学了这些,发现有什么意义?下面写个文档管理系统例子来说明,需求见下图

1.编写Document接口

1

2

3

4 publicinterfaceDocument{

publicvoidread();

publicvoidwrite();

} 2、编写实现类WordDocument,ExcelDocument,PDFDocument

1

2

3

4

5

6

7

8

9 publicclassWordDocumentimplementsDocument{

publicvoidread(){

System.out.println("wordread");

}

publicvoidwrite(){

System.out.println("wordwrite");

}

} 3、编写文档管理系统DocumentManager

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20 publicclassDocumentManager{

privateDocumentdocument;

publicvoidsetDocument(Documentdocument){

this.document=document;

}

publicDocumentManager(){

}

publicDocumentManager(Documentdocument){

super();

this.document=document;

}

publicvoidread(){

this.document.read();

}

publicvoidwrite(){

this.document.write();

}

} 4、编写测试类DocumentTest

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 /

利用ioc和di能做到完全的面向接口编程



/

publicclassDocumentTest{

/

Documentdocument=newWordDocument();

这行代码是不完全的面向接口编程,因为等号的右边出现了具体的类

/

@Test

publicvoidtestDocument_NOSPRING(){

Documentdocument=newWordDocument();

DocumentManagerdocumentManager=newDocumentManager(document);

documentManager.read();

documentManager.write();

}

/

在代码端不知道Document是由谁来实现的,这个是由spring的配置文件决定的


class="com.itheima10.spring.iocdi.www.hunanwang.netdocument.DocumentManager">













/

@Test

publicvoidtestDocument_Spring(){

ApplicationContextcontext=

newClassPathXmlApplicationContext("applicationContext.xml");

DocumentManagerdocumentManager=(DocumentManager)context.getBean("documentManager");

documentManager.read();

documentManager.write();

}

} 从上面可以看出不适用spring和适用spring的区别

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19








class="com.itheima10.spring.iocdi.document.DocumentManager">















建立工程目录



编码:1、创建Dao层建立PersonDao接口和实现类PersonDaoImpl

1

2

3

4

5

6

7

8

9

10 publicinterfacePersonDao{

publicvoidsavePerson();

}

publicclassPersonDaoImplimplementsPersonDao{

@Override

publicvoidsavePerson(){

System.out.println("saveperson");

}

} 2、建立service层,PersonService接口与PersonServiceImpl实现类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17 publicinterfacePersonService{

publicvoidsavePerson();

}

publicclassPersonServiceImplimplementsPersonService{

privatePersonDaopersonDao;

publicvoidsetPersonDao(PersonDaopersonDao){

this.personDao=personDao;

}

@Override

publicvoidsavePerson(){

this.personDao.savePerson();

}

} 3、建立Action,PersonAction类

1

2

3

4

5

6

7

8

9

10

11 publicclassPersonAction{

privatePersonServicepersonService;

publicvoidsetPersonService(PersonServicepersonService){

this.personService=personService;

}

publicvoidsavePerson(){

this.personService.savePerson();

}

} 4、配置applicationContext.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14





















5、编写测试类testMVC

1

2

3

4

5

6

7

8

9 publicclassMVCTest{

@Test

publicvoidtestMVC(){

ApplicationContextcontext=

newClassPathXmlApplicationContext("applicationContext.xml");

PersonActionpersonAction=(PersonAction)context.getBean("personAction");

personAction.savePerson();//saveperson

}

} 上述实例很清楚的展现出了spring的面向接口编程,service层只需调用dao层的接口,而不需要关注于dao层的实现类,action也只需调用service的接口,而不需要关注service的实现类。























献花(0)
+1
(本文系白狐一梦首藏)