JavaWebSpring依赖注入深入学习_第1页
JavaWebSpring依赖注入深入学习_第2页
JavaWebSpring依赖注入深入学习_第3页
JavaWebSpring依赖注入深入学习_第4页
JavaWebSpring依赖注入深入学习_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JavaWeb Spring依赖注入深入学习一、依赖注入(DI)依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setting方法的形式。1 构造器注入1 使用构造器注入使用xml的注入方式A. 通过参数的顺序张三56B. 通过参数的类型56张三具体实例假如现在要对一个Person类注入参数,Student是一个另外一个类。12345678910111213141516public class Person private String pid;private String name;private Student student;

2、public Person(String pid, Student student)this.pid= pid;this.student = student;public Person(String pid, String name)this.pid = pid; = name;配置applicationContext.xml,假如不进行参数配置,则报错,找不到相应的构造器。配置了相应的参数,则应在类中声明相应的构造函数。12345678910111213141516171819202122编写测试类DIXMLConstructorTest ,进行断点调试,将会发现根据配置的

3、参数,进入的构造函数是Person(String pid, Student student)12345678public class DIXMLConstructorTest Testpublic void test1()ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);Person person = (Person) context.getBean(person); 2 使用属性setter方法进行注入使用xml的注入方式:A. 简单Bean的注入 简单Bean包括两

4、种类型:包装类型和String12345 B. 引用其他Bean?12341.1 装配list集合1234567list1list21.2 装配set集合1234567list1list21.3 装配map12345678910map01map02 map中的的数值和以及的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的。1.4 装配Properties123456prop1prop2 具体实例1.创建两个对象Person和Student12345678910111213141516171819202122package xgp.spring.demo;import j

5、ava.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class Person private String pid;private String name;private Student student;private List lists;private Set sets;private Map map;private Properties properties;private Object objects;public Person()System.out.pr

6、intln(new person);/省略getter和setter方法1234567891011package xgp.spring.demo;public class Student public Student()System.out.println(new student);public void say()System.out.println(student);配置applicationContext.xml文件123456789101112131415161718192021222324252627282930313233343536373839404142434445464748

7、4950515253545556575859list1list2set1set2map1prop1prop2aabb编写测试类DIXMLSetterTest12345678910111213141516171819202122232425262728293031323334353637package xgp.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import support

8、.ClassPathXmlApplicationContext;import xgp.spring.demo.Person;public class DIXMLSetterTest /* spring 容器做的事情:* 1、spring容器做了什么?(1)启动spring容器* (2)为person和student两个bean创建对象* (3)解析property的name属性,拼接setter方法,解析property的* value或者ref属性,给setter方法传递参数,利用反射技术给对象赋值。* (4)从spring容器中,把对象提取出来,对象调用方法。* 2、spring容器执行顺

9、序是什么? */Testpublic void test1()ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);Person person = (Person) context.getBean(person);System.out.println(person.getPid();System.out.println(person.getName();System.out.println(person.getLists();System.out.println(perso

10、n.getSets();System.out.println(person.getMap();System.out.println(person.getObjects().length);/*1王五list1, list2, xgp.spring.demo.Student76a9b9cset1, set2, xgp.spring.demo.Student76a9b9centry1=map1, entry2=map22*/spring容器的执行顺序1.都是默认设置2.设置student(lazy-init=true)3.设置person(lazy-init=true)总结 可以采用两种方法注入参

11、数,构造器要写对应的构造函数,setter要生成相应的setter方法,并编写默认的构造器。2.5 IOC与DI的意义学了这些,发现有什么意义?下面写个文档管理系统例子来说明,需求见下图1.编写Document 接口1234public interface Document public void read();public void write();2、编写实现类WordDocument ,ExcelDocument ,PDFDocument123456789public class WordDocument implements Documentpublic void read() Sys

12、tem.out.println(word read);public void write() System.out.println(word write);3、编写文档管理 系统 DocumentManager1234567891011121314151617181920public class DocumentManager private Document document;public void setDocument(Document document) this.document = document;public DocumentManager() public DocumentM

13、anager(Document document) super();this.document = document;public void read()this.document.read();public void write()this.document.write();4、编写测试类DocumentTest1234567891011121314151617181920212223242526272829303132333435363738394041/* 利用ioc和di能做到完全的面向接口编程*/public class DocumentTest /* Document docume

14、nt = new WordDocument();* 这行代码是不完全的面向接口编程,因为等号的右边出现了具体的类*/Testpublic void testDocument_NOSPRING()Document document = new WordDocument();DocumentManager documentManager = new DocumentManager(document);documentManager.read();documentManager.write();/* 在代码端不知道Document是由谁来实现的,这个是由spring的配置文件决定的* */Testp

15、ublic void testDocument_Spring()ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);DocumentManager documentManager =(DocumentManager)context.getBean(documentManager);documentManager.read();documentManager.write();从上面可以看出不适用spring和适用spring的区别1234567891011121314151

16、6171819使用spring只需要在applicationContext中配置相应的对象,而不需要关注具体的实现类,实现完全的面向接口编程,这也是为什么spring能够和这么多工具集成的原因。2.6 mvc实例模拟structs2需求描述建立工程目录编码: 1、创建Dao层 建立PersonDao接口和实现类PersonDaoImpl12345678910public interface PersonDao public void savePerson();public class PersonDaoImpl implements PersonDao Overridepublic void

17、savePerson() System.out.println( save person);2、建立service层,PersonService接口与PersonServiceImpl实现类1234567891011121314151617public interface PersonService public void savePerson();public class PersonServiceImpl implements PersonServiceprivate PersonDao personDao;public void setPersonDao(PersonDao person

18、Dao) this.personDao = personDao;Overridepublic void savePerson() this.personDao.savePerson();3、建立Action,PersonAction类1234567891011public class PersonAction private PersonService personService;public void setPersonService(PersonService personService) this.personService = personService;public void savePerson()this.personService.savePerson();4、配置applicationContext.xml1234567891011121314 5、编写测试类testMVC123456789public class MVCTest Testpublic void testMVC()Applicat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论