版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
软件开发架构平台技术CH12Spring&IOC回顾持久化框架(ORM)的概念MyBatis框架Hibernate框架目录Spring框架简介控制反转和依赖注入使用IOC进行Web应用开发Spring简介Spring
:Spring是一个开源框架,于2003年兴起的一个轻量级的Java开发框架。Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。目标:使现有技术更加易用,推进编码最佳实践。内容:依赖注入容器,AOP实现(声明式事务),DAO/ORM支持,Web集成。Spring之父RodJohnsonSpring框架创始人,interface21公司CEO。丰富的c/c++开发和金融行业背景。1996年开始关注Java服务器端技术,Servlet2.4和JDO2.0专家组成员。2002年著写《Expertone-on-oneJ2EEDevelopmentwithoutEJB》,对企业级应用开发,特别是Java领域。核心理念:Don’tReinventtheWheel.依赖注入的来源复杂的软件系统面向对象使系统的实现变得简单当系统复杂到一定程度时,仅仅面向对象不能降低复杂性依赖注入的来源组件化核心思想:解耦合,将组件的构建和组件的使用分开,实现每个组件块时只考虑组件内部的事情,要点是明确和定义组件间的接口。组件的使用组件的生产接口的定义运行时注入依赖注入——案例分析比如要完成一个小项目,这个项目的功能是在用户界面展现一个字符串(如Helloworld)。这个项目至少包含3个功能模块:从持久数据(DB)中读取字符串;对读取的数据进行处理(编码、格式等);将数据展现在用户界面(命令行、GUI、HTML)。依赖注入——案例分析方法一:最基本的面向对象的实现方式。针对三个功能模块分别编写三个类:FileHelloStr类:从持久化数据(属性文件)读取信息。HelloWorld类:业务逻辑,添加Software信息。HelloWorldClient类:将信息输出到命令行。依赖注入——案例分析方法一:FileHelloStr类public
classFileHelloStr{
privateStringpropfilename;
publicFileHelloStr(Stringpropfilename){
this.propfilename=propfilename;}
publicStringgetContent(){Stringhelloworld="";
try{Propertiesproperties=newProperties(); InputStreamis= getClass().getClassLoader().getResourceAsStream(propfilename);properties.load(is);is.close();helloworld=properties.getProperty(“helloworld"); }catch(FileNotFoundExceptionex){ ex.printStackTrace(); }catch(IOExceptionex){ ex.printStackTrace(); }
returnhelloworld;}}依赖注入——案例分析方法一:HelloWorld类public
classHelloWorld{
publicStringgetContent(){FileHelloStrfhStr=newFileHelloStr("perties");Stringhellworld=fhStr.getContent()+"Software";
returnhellworld;}}方法一:HelloWorldClient类public
classHelloWorldClient{
public
static
voidmain(String[]args){HelloWorldhw=newHelloWorld();System.out.println(hw.getContent());}}依赖注入——案例分析方法一分析HelloWorld类依赖于FileHelloStr类HelloWorldClient类依赖于HelloWorld类系统的各部分之间相互依赖,导致可维护性和扩展性差,不适合大型、企业级的应用开发。解决方案用面向接口编程的思想,将业务逻辑层(HelloWorld)和DAO层(FileHelloStr)的耦合消除。将HelloWorld类中对具体的FileHelloStr类的操作,委托给外部类(HelloWorldClient)。依赖注入——案例分析方法二:FileHelloStr类和HelloStr接口public
interface
HelloStr{
publicStringgetContent();}public
interface
HelloStr{
publicStringgetContent();}public
class
FileHelloStrimplementsHelloStr{
privateStringpropfilename;
publicFileHelloStr(Stringpropfilename){
this.propfilename=propfilename;}
publicStringgetContent(){Stringhelloworld="";...... //具体代码如方法一
returnhelloworld;}}依赖注入——案例分析方法二:HelloWorld类public
classHelloWorld{
privateHelloStrhStr;
publicHelloWorld(HelloStrhStr){
this.hStr=hStr;}
publicStringgetContent(){
return
hStr.getContent()+"Software";}}方法二:HelloWorldClient类public
classHelloWorldClient{
public
static
voidmain(String[]args){HelloStrfhStr=newFileHelloStr("perties");HelloWorldhw=newHelloWorld(fhStr);System.out.println(hw.getContent());}}依赖注入——案例分析方法二分析优点:业务逻辑(HelloWorld类)和数据访问(HelloStr接口)完全解耦(通过构造注入)缺点:界面逻辑(HelloWorldClient类)和另外两层均有依赖。解决方案到系统外部来解决这种依赖关系——工厂模式。构建一个HelloworldFactory工厂类来处理类之间的耦合关系。依赖注入——案例分析方法三:HelloWorldFactory类public
classHelloWorldFactory{
public
staticHelloWorldgetFileHelloWorld(){HelloStrhStr=newFileHelloStr("perties");HelloWorldhw=newHelloWorld(hStr);
returnhw;}}方法三:HelloWorldClient类public
classHelloWorldClient{
public
static
voidmain(String[]args){HelloWorldhw=HelloWorldFactory.getFileHelloWorld();System.out.println(hw.getContent());}}依赖注入——案例分析方法三分析优点:业务逻辑(HelloWorld类)、数据访问(HelloStr接口)和界面逻辑(HelloWorldClient类)三者之间完全解耦。HelloWorldFactory类负责创建和集成客户应用所需的对象。借助依赖注入(DI,DependencyInjection)和工厂模式实现了控制反转(IoC,InversionofControl)。缺点:这种控制反转依然需要硬编码来实现。解决方案借助框架及外部配置文件来实现解耦和控制反转。依赖注入——案例分析方法四:配置文件applicationContext.xml<beanname="fileHelloWorld"class="com.openv.spring.HelloWorld"><constructor-arg> <refbean="fileHello"/></constructor-arg></bean><beanname="fileHello"class="com.openv.spring.FileHelloStr"><constructor-arg> <value>perties</value></constructor-arg></bean>方法四:HelloWorldClient类public
classHelloWorldClient{
publicHelloWorldClient(){Resourceresource=newClassPathResource("applicationContext.xml");BeanFactoryfactory=newXmlBeanFactory(resource);HelloWorldhw=(HelloWorld)factory.getBean("fileHelloWorld");System.out.println(hw.getContent());}
public
static
voidmain(String[]args){
newHelloWorldClient();}}依赖注入的方式依赖注入有两种常见的方式:构造注入设值注入(setter注入)set注入<beanid="userBiz"class="...UserBizImpl"><propertyname="userDAO"ref="userDAO"/></bean>构造注入<beanname="fileHelloWorld"class="com.openv.spring.HelloWorld"><constructor-arg> <refbean="fileHello"/></constructor-arg></bean><beanname="fileHello"class="com.openv.spring.FileHelloStr"><constructor-arg> <value>perties</value></constructor-arg></bean>两种注入方式比较设值注入的优点与传统JavaBean写法类似,依赖关系直观、自然对于复杂依赖关系,设值注入能避免构造器过于复杂在依赖关系(参数)可选时,能避免构造器重载构造注入的优点构造注入能表达依赖关系的顺序构造注入方式中不使用setter方法,能避免后续代码对依赖关系的破坏构造注入方式中依赖关系只能由组件创建者决定,对组件调用者而言,依赖关系完全透明,更符合高内聚、低耦合的原则。SpringIoC的核心组件BeanFactory位于org.springframework.beans.factory包中。使开发者借助于配置文件(如XML或属性文件),能够实现对JavaBean的配置和管理。主要用于JavaSE应用。ApplicationContext位于org.springframework.context包中。ApplicationContext构建在BeanFactory基础之上。除了具有BeanFactory的功能之外,还添加了大量功能,如Spring
IoC集成、国际化资源、事件机制。主要用JavaEE应用。依赖注入使用范例如何开发一个打印机?打印机功能的实现依赖于墨盒和纸张。步骤:
1、定义墨盒和纸张的接口标准。
2、使用接口标准开发打印机。
3、组装打印机。
4、运行打印机。使用依赖注入第一步:定义组件接口墨盒接口:Ink纸张接口:PaperpublicinterfaceInk{publicStringgetColor(intr,intg,intb);}publicinterfacePaper{publicstaticfinalStringnewline="\r\n";/***输出字符到纸张
*/publicvoidputInChar(charc);/***得到输出到纸张上的内容
*/publicStringgetContent();}使用依赖注入第二步:使用接口开发打印机publicclassPrinter{ publicInkink=null; publicPaperpaper=null; publicvoidprint(Stringstr){ System.out.println("使用"+
ink.getColor(255,200,0).+"颜色打印"); for(inti=0;i<str.length();++i){ //逐字符输出到纸张
paper.putInChar(str.charAt(i)); } System.out.print(paper.getContent());//将纸张的内容输出
}}使用依赖注入第三步:组装打印机
a.为了方便组装,给Printer类的ink和paper属性添加setter属性。publicclassPrinter{ publicInkink=null; publicPaperpaper=null; ...... publicvoidsetInk(Inkink){ this.ink=ink;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 吉艺编导考试题目及答案
- 电气安规考试题库及答案
- 僵尸车停放点巡查制度
- 2026六年级数学下册 百分数运算能力
- 项目管理专业试题及答案
- 企业管理部车辆维修保养制度
- 企业内部会计控制的一项重要制度是应收账款内部控制制度
- 山西运城市2026届高考考前模拟测试语文(运城一模)+答案
- 五台县文昌学校制度
- 科室病案奖惩制度范本
- 蛋白质能量营养障碍(儿科学)
- 苏少版美术六下《头饰和帽子》教案设计
- 初高中音标衔接课含发音单词学案-初高中英语衔接
- DB11-1134-2014高压电力用户安全用电规范
- QC成果提高预埋套管的安装质量
- 真空炉操作规程
- 物理竞赛大纲(新)
- 混凝土基本知识简介_PPT
- 北京化工大学 管理学 电子教案 第1章 管理与管理学
- (高清版)建筑地面工程防滑技术规程JGJ_T 331-2014
- 数学教学目标的设定
评论
0/150
提交评论