Chapter2Chapter2_第1页
Chapter2Chapter2_第2页
Chapter2Chapter2_第3页
Chapter2Chapter2_第4页
Chapter2Chapter2_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

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

文档简介

Chapter2 Spring依赖注入*课程回顾*1 Spring介绍及基本概念2 IOC HelloWorld3 实现工厂模式IOC容器 *教学导航*1 ApplicationContext接口2 bean 的作用域及生命周期方法3 依赖注入实现方法1、 ApplicationContext接口在使用Spring的时候,我们经常需要先得到一个ApplicationContext对象,然后从该对象中获取我们配置的Bean对象。 ApplicationContext隶属于org.springframework.context,是SpringFramework中Bean的管理者,为SpringFramework的诸多功能提供支撑作用。BeanFactory系列接口简介(了解):public interface BeanFactoryBeanFactory 是 Spring 管理 Bean 的最顶层接口,是一个 Bean 容器, 管理一系列的 bean,每一个 bean 使用一个String 类型的 name(或称之为id) 来唯一确定,这些 Bean 可以是 prototype 的或者 singleton的 。Spring 提倡使用依赖注入(Dependency Injection) 的方式装配 Bean。public interface HierarchicalBeanFactory extends BeanFactoryBeanFactory的子接口HierarchicalBeanFactory是一个具有层级关系的Bean 工厂,拥有属性parentBeanFactory。当获取 Bean对象时,如果当前BeanFactory中不存在对应的bean,则会访问其直接 parentBeanFactory 以尝试获取bean 对象。public interface ListableBeanFactory extends BeanFactoryListableBeanFactory 继承了BeanFactory,实现了枚举方法可以列举出当前BeanFactory中所有的bean对象而不必根据name一个一个的获取。#ApplicationContext接口作用#ApplicationContext接口继承众多接口,集众多接口功能与一身,为Spring的运行提供基本的功能支撑。根据程序设计的“单一职责原则”,其实每个较顶层接口都是“单一职责的”,只提供某一方面的功能,而ApplicationContext接口继承了众多接口,相当于拥有了众多接口的功能:1、 首先,它是个BeanFactory,可以管理、装配bean2、 其次,它是一个ResourceLoader,可以加载资源文件3、 它可以管理一些Message实现国际化等功能4、 它还可以发布事件给注册的Listener,实现监听机制ApplicationContext两个比较常用的实现类:ClassPathXmlApplicationContext: 使用类路径方式初始化ioc容器(推荐使用)FileSystemApplicationContext: 使用文件系统的方式初始化ioc容器/* * 方式一:类路径方式加载,默认在类路径的根目录下(也就是src目录下) */ApplicationContext ac = new ClassPathXmlApplicationContext(applicationContext.xml);/* * 方式二:使用文件系统的方式初始化ioc容器 */ApplicationContext ac = new FileSystemXmlApplicationContext(E:workspaces2.spring-ioc-helloworldsrcapplicationContext.xml);#bean作用域#这里的 scope 就是用来配置 spring bean 的作用域,它标识 bean 的作用域。在spring配置文件中,bean标签通过scope属性定义对象的创建方式,具体如下:scope=singleton 默认方式 采用单例模式创建对象scope=prototype 采用原型模式创建对象,对象是多例的scope=request 在web中request的范围scope=session 在web中session的范围scope=global session 在web中application的范围#bean生命周期方法#Spring为了满足开发者在执行某方法之前或者在结束某个任务之前需要操作的一些业务,则提供了init-method和destroy-method 这两个属性,这两个属性需要加载在bean节点中。1、 创建javaWeb工程项目,导入Spring ioc依赖的jar包(参见SpringTwo项目 以客户模块为例)2、 编写DAO接口和实现类CustomerDao接口:/* * 客户模块 1、save() 保存 */public interface CustomerDao public void save();CustomerDaoImpl实现类(实现类加上init-方法和destroy-方法):public class CustomerDaoImpl implements CustomerDao Overridepublic void save() System.out.println(把客户数据保存到mysql数据!);/* * 执行save之前执行 */ public void inits() System.err.println(这里在 执行save之前执行! ); /* * 销毁对象前调用 */ public void shutdown() System.err.println(销毁 CustomerDaoImpl 对象实例 前调用 shutdown()方法); 3、 编辑spring配置文件,applicationContext.xml 4、 增加Service服务测试层,模拟服务层编写测试代码/* * 保存客户信息 */Testpublic void save()ApplicationContext ac = new ClassPathXmlApplicationContext(applicationContext.xml);CustomerDao customerDao = (CustomerDao)ac.getBean(customerDao); customerDao.save(); /IOC容器关闭 也就摧毁customerDao实例对象(ClassPathXmlApplicationContext) ac).close(); 5、 运行测试类的方法,结果为:2、 依赖注入实现方法Spring IOC工作原理IOC实现了工厂模式,通过读取applicationContext.xml文件中标签的类,注入到IOC容器中,通过set方法与构造等方法进行注入,产生一个BeanFactory,BeanFactory通过getBean()方法获取对象依赖注入有三种方式:属性注入构造方法注入工厂方法注入(很少使用,不推荐使用)或加上注解实现注入(后面介绍)#属性注入#这是目前最流行最简单的DI注入方法,通过 setter 方法注入Bean 的属性值或依赖的对象,不过有些人叫setter方法注入,在 Spring 的配置文件中通过 property属性进行注入。1、 创建javaWeb工程项目,导入Spring ioc依赖的jar包(参见SpringTwo项目 beanioc包下以客户模块为例)2、 编写DAO接口和实现类CustomerDao接口:/* * 客户模块 1、save() 保存 */public interface CustomerDao public void save();CustomerDaoImpl实现类:public class CustomerDaoImpl implements CustomerDao Overridepublic void save() System.out.println(把客户数据保存到mysql数据!);3、 编写Service接口与实现类CustomerService接口:/* * 客户模块 1、save() 保存 */public interface CustomerService /* * 保存客户信息 */public void save();CustomerServiceImpl类:public class CustomerServiceImp implements CustomerServiceprivate CustomerDao customerDao;/关键在这里,提供CustomerDao属性作为参数的setter方法,待会在配置文件进行注入public void setCustomerDao(CustomerDao customerDao) this.customerDao = customerDao;OverrideTestpublic void save() customerDao.save();4、 编辑spring配置文件,applicationContext.xml 5、 action测试层,测试代码public class CustomerAction Testpublic void save() ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);CustomerService customerService = (CustomerService)context.getBean(customerService);customerService.save();6、 运行测试类的方法,结果为:#构造方法注入#通过构造方法注入,就相当于给构造方法的参数传值,它保证了 Bean 实例在实例化后就可以使用。1、 创建javaWeb工程项目,导入Spring ioc依赖的jar包(参见属性注入)2、 编写DAO接口和实现类(参见属性注入)3、 编写Service接口与实现类CustomerService接口(参见属性注入):CustomerServiceImpl类:public class CustomerServiceImp implements CustomerServiceprivate CustomerDao customerDao;/*构造注入 * 关键在这里,提供CustomerDao作为参数的构造方法,待会在配置文件进行注入 * param customerDao */public CustomerServiceImp(CustomerDao customerDao) this.customerDao = customerDao;OverrideTestpublic void save() customerDao.save();4、 编辑spring配置文件,applicationContext.xml 5、 action测试层,测试代码(参见属性注入):7、 运行测试类的方法,结果为:IOC容器管理对象特点创建对象的时机单例:单例对象在spring创建IOC容器时被创建多例:多列对象使用到的时候被创建对象单实例延迟加载bean标签通过设置lazy-init=true,可以使得单例对象第一次使用时被创建scope=prototype时候,lazy-init=true失效对象初始化与销毁init-method=方法名 此方法在对象创建完成后被调用destroy-method=方法名 此方法在对象被回收之前调用Spring 提供了不同数据类型的注入方法(1) 值类型(String、int、double 等)(2) 引用类型(ref:对象ID)(3) List 或数组类型(4) set 类型(5)(6) map 类型(6)空字符串和 null 类型如果想注入空字符串直接写,如果直接注入 null 直接写。*本章作业*1、 选择题1、以下说法正确的是( )ASpring 集成了很多功能模块,是一个重量级框架BSpring 中的依赖注入能够让代码之间的依赖关系更紧密CSpring 的面向接口进行依赖注入能够很大程度地体现代码的灵活性DSpring 的依赖注入功能能够替代面向对象的设

温馨提示

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

评论

0/150

提交评论