javaee企业级分布式高级架构师开课吧08spring5讲义_第1页
javaee企业级分布式高级架构师开课吧08spring5讲义_第2页
javaee企业级分布式高级架构师开课吧08spring5讲义_第3页
javaee企业级分布式高级架构师开课吧08spring5讲义_第4页
javaee企业级分布式高级架构师开课吧08spring5讲义_第5页
已阅读5页,还剩89页未读 继续免费阅读

付费下载

下载本文档

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

文档简介

IoCXMLIoCXMLDIIoCSpring(XMLSpringSpringSpringAOPSpringAOPXMLSpringSpringJDBCSpringSpringSpringMybatisSpringSpring:SpringSpringFramework(spring框架)***为衡量模块独立程度的标准。划分模块的一个准则就是高内聚低耦合***Springspringspringspringframework:jarjarSpringSpring概念介 SpringAOP:AspectOrientedProgramming,面向切面编程。在不修改目标对象的源代码情况下,增强IoC容器中Bean的功能。DI:DependencyInjection,依赖注入。SpringBean态的将依赖对象注入到Bean组件中!!SpringIoCSpringIoC******SpringIoCSpringIoC***IoC***IoC***IoCBeanIoCIoC******IoCSpringBeanBean实例)SpringBeanFactoryApplicationContextBean***BeanFactory工厂(SpringBeanBeanFactoryBean***其实通过源码分析,不管是BeanFactory还是ApplicationContext,其实最终的底层BeanFactory都是DefaultListableBeanFactoryApplicationContextBeanFactory******Bean***ApplicationContext是加载完applicationContext.xml时,就创建具体的只对WebIoC它是从类的根路径下加载配置文 推荐使用这JavaIoC:(了解ApplicationContextApplicationContextcontextnewClassPathXmlApplicationContext(xmlWebIoC:(重点web.xmlContextLoaderListenerContextConfigLocation***ContextLoaderListener器会在web容器启动的时候,触发***contextInitialized()方调用initWebApplicationContext()方法该方法负责创建Spring容器(DefaultListableBeanFactory)。【Web三类八种器】***springContextLoaderListenerSpring*创建:第一次*销毁:调用invalidate();关闭;过 HttpSessionBindingListener(HttpSession和JavaBeanweb.xml调用ContextLoaderListenercontextInitializdWebContextLoaderinitWebApplicationContext继续调用ContextLoaderconfigureAndRefreshWebApplicationContext该方法中调用最终初始化Bean的refresh方法Springspring主流程ApplicationContextApplicationContextcontext=newApplicationContext的refresh方法:初始化spring容器的代publicvoidrefresh()throws //1、Preparethiscontextforrefreshing. lthesubclasstorefreshtheinternalbeanfactory.ConfigurableListableBeanFactorybeanFactory=obtainFreshBeanFactory();//3、Preparethebeanfactoryforuseinthiscontext.try//4、Allowspost-processingofthebeanfactoryincontextsubclasses. //5、Invokefactoryprocessorsregisteredasbeansinthecontext. //6、Registerbeanprocessorsthatinterceptbeancreation.//7、Initializemessagesourceforthiscontext.//8、Initializeeventmulticasterforthiscontext.//9、Initializeotherspecialbeansinspecificcontextsubclasses.//10、Checkforlistenerbeansandregisterthem.//11、Instantiateallremaining(non-lazy-initsingletons.//12、Laststep:publishcorrespondingevent.}catch ceptionex)logger.warn("Exceptionencounteredduringcontextinitialization-"+"cancellingrefreshattempt:"+ex);}//Destroyalreadycreatedsingletonstoavoiddanglingresources.//Reset'active'flag.//Propagateexceptiontothrow}finally//ResetcommonintrospectioncachesinSpring'score,since//mightnoteverneedmetadataforsingletonbeansanymore...}}}BeanFactoryBeanFactory子流程(从主流程refresh方法中的第二步开始调 ApplicationContext中的obtainFreshBeanFactory方调 BeanDefinition子流程(loadDefinitions方法子流程 此处依次调用多个类的loadBeanDefinitions方法 XmlBeanDefinitionReader),一直调用到XmlBeanDefinitionReader类的doLoadBeanDefinitions对于 方法不是我们关注的重点,我们进入到该类registerBeanDefinitions此处有两个地方是我们关注的:一个createRederContext方法,一个是 Reader类的registerBeanDefinitions方法先进入createRederContext方法看看至此,14个NamespaceHandlerResolver初始化成功。然后我们再进入 ReaderregisterBeanDefinitions继续进入到该类的parseBeanDefinitions我们看到有两种解析方案,先看看 ement方BeanDefinitionParserDelegateparseCustomElement(AOP、tx的解析都是在该步骤中完成的getNamespaceURI方法的作用一目了然,我们就不去追踪了,接下来我们进入类的它实现了自定义到处理类的工作,不过NamespaceHandler是一个接口,具initAopNamespaceHandler了解一下init的作用,其中aop:config是由ConfigBeanDefinitionParser类进行至此,我们了解到了xml中的aop都是由哪些类进行处理的了。不过init方法只到BeanDefinitionParserDelegate类的parseCustomElement方法看看我们看到,最后一行执行了parse方法,那么parse方法,在哪呢?我们需要到NamespaceHandlerSupportNamespaceHandlerAopNamespaceHandlerNamespaceHandlerSupport法也会继承到AopNamespaceHandler类中。至此,整个XML文档的解析工作,包括bean以及自定义如何解析后续具体想了解哪个自定义的处理逻辑,可以自行去查xxxNamespaceHandler.2图示BeanfinishBeanFactoryInitializationDefaultListableBeanFactorypreInstantiateSingletons BeanFactory类的doGetBean方法,这个方接着进入 我们终于找到的地方了,进入doCreateBean方法看看,该方法我们关注两块对于如何创建Bean的实例,和填充属性,暂时先不去追踪了,我们先去看看initializeBeanBeanPostProcessorAOP至此,如何创建Bean,以及AOP在哪产生的步骤,我们已经分析过了SpringIoCXML POM<project<project"" 组件中的4个依赖--><!--单元测试Junit<!--配置Maven的JDK编译级别--> spring(只编写配置文件头<?xml<?xmlversion="1.0"encoding="UTF-<beansxmlns="""在在Spring的XML配置文件中配置一个,该最终会被加载为一个BeanDefition对象(描述对象信息******编写UserService***将UserServiceSpringIoC***SpringIoCUserService编写接口编写实现类XMLbean详 ***用于配置对象让spring来创建的。*** ***class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数singleton默认值,单例的(在整个容器中只有一个对象prototyperequest:WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.session:WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.global :WEB项目中,应用在Portlet环境.如果没有Portlet环globalSession相当于***destroy-methodDataSourcedestroy-method=“close”***bean ***对 :当应用卸载,销毁容器时,对象就被销毁了***每 对象时,都会重新创建对象实例***对 :当对象长时间不用时,被java 回收器回收了bean第一种:使用默认无参构造函数(重点***如果bean<beanid="userService"第二种:静态工厂(了解*publicclassStaticFactorypublicstaticUserServicereturnnew}}<!--此种方式是:<!--此种方式是:使用StaticFactory类中的静态方法createUserService创建对象,并存入spring容器id属性:指定bean的id,用于从容器中获取class属性:指定静态工厂的全限定类名factory-method--<beanid="userService"class="com.kkb.spring.factory.StaticFactory"factory-第三种:实例工厂(了解publicclassInstanceFactorypublicUserServicereturnnew}}}<!--此种方式是:***先把工厂的创建交给spring来管理。***然后在使用工厂的bean来调用里面的方法factory-bean属性:用于指定实例工厂bean的id。factory-method属性:用于指定实例工厂中创建对象的方法。-- SpringDI***Bean***属性分为:简单类型(8String)的属性、POJO***依赖注入:DependencyInjection。它是spring框架ioc的方法。那这种业务层和持久层的依赖关系,在使用spring之后,就让spring来依赖注入的方式(publicpublicclassUserServiceImplimplementsUserServiceprivateintprivateStringpublicUserServiceImpl(intid,Stringname)this.id==}publicvoidsaveUser() }}}service***value:它能赋的值是基本数据类型和String类型***ref:它能赋的值是其他bean的--

set(重点******set又分为手动装配方式注入和自动装配方式注入方式set***自动装配方式(注解方式,后面讲解):@Autowired注解、@Resource注***@Resource:一部分功能是查找实例,从spring容器中根据Beanp(set11.步骤一:需要先引入p名称空间*在 "pp:属性名p:属性名-ref3.步骤三:测试*<bean"""<beanid="car2"class="com.kkb.spring.demo.Car2"依赖注入不同类型的属性(简单类型<bean<beanid="userService"<property<propertyname="id"<propertyname="name"类型ref就是reference的缩写,是的意思<bean<beanid="userService"<propertyname="userDao"ref="userDao"></constructor-<beanid="userDao"集合类型(数组1.如果是数组或者List1.如果是数组或者List<beanid="collectionBean"<propertyvaluePOJOSet<property//如果集合内是简单类型,使用value ,如果是POJO类型,则使Map<property<entry 2"<entry "<entrykeyProperties<property<property<prop<propSpringIoC和DI******学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置***spring的xml配置内容改为使用IoC***第一步:spring***第一步:spring配置文件中,配 IoC(创建对象相当于:<beanid=""@Component******把资源让spring来管理。相当于在xml中配置一个bean***value:指定bean的id如果不指定value属性,默认bean的id是当前类的类名。首字母小写注解***他们三个注解都是针对@Component***他们三个注解都是针对@Component***细节:如果注解中有且只有一个属性要赋值时,且名称是value,value在赋值是可以DI(依赖注入相当于:<propertyname=""***spring***nullrequired性为false,如:@Autowired(required=false)注解进行使用******在自动按照类型注入的基础之上,再按照Bean的id注入。***它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入***默认按照***默认按照名称(byName)name***J2EE***name找不到与名称匹配的bean时才按照类型进行装配。***name推荐使用@ResourceJ2EEspring相当于:<propertyname******String@Value(“${name}”)//namepropertieskeyprivateStringname;相当于相当于<beanid=""class***指定bean***取值:singletonprototyperequestsession相当于:相当于:<beanid=""class=""init-method=""destroy-method=""/>@PostConstruct和XML配置简单 方便(我们找到类,就相当于找到了对应的配置)***XML的优势:***Spring管理Bean方式的比较:Spring写到此处,基于注解的不开spring的xml配置文件,那么能不能不写这个applicationContext.xml,所有******想能不能将以下这些bean的配置都从xml中去掉,并且最终将XML也去掉***xml注解扫描配置(能不能去掉 ponent-scanbase-Bean(比如:SqlSessionFactoryBasicDataSource>>XMLApplicationContextXMLApplicationContextcontextnew***Spring3.0,@Configuration配置类,可xml***配置类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。属性示例代码publicclassSpringConfiguration//springpublicSpringConfiguration(){}}***@Beanspring***bean***beanDruidDataSource、***name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id) 注解默认作用域为单例 作用域,可通过publicclassSpringConfiguration//springpublic}publicUserServicereturnnew}}}}}publicvoidsaveUser() }publicclassUserServiceImplimplementsUserService注解***该注解是编写在类上面的,一般配合@Configuration***valuebasePackagesBean(Service***publicpublicclassSpringConfigurationpublicSpringConfiguration(){}}publicUserServiceuserService(){returnnewUserServiceImpl(1,"}publicclassJdbcConfig{privateStringdriver;privateStringurl;***加载properties***相当于 valuepropertiesprivateprivateStringusername;privateStringpassword;publicDataSourcecreateDataSource()tryComboPooledDataSourceds=newComboPooledDataSource();ds.setDriverClass(driver);returnds;}catch(Exceptione)thrownew}}}propertiesproperties***之前使用XML配置的时候是如何解决@ComponentScan(basePackages="com.kkb.spring")@Import({JdbcConfig.class})publicclassSpringConfiguration}***spring***在引入其他配置类时,可以不用再写@Configuration注解。当然,写上也没问publicpublicclass}JavaApplicationContextcontext=ApplicationContextcontext=newUserServiceservice=context.getBean(UserService.class);应用</web-Springspring***表现层:spring***业务层:springBean,***持久层:spring配置文件,只想管理持久层的Bean,并且还有需要管理数据源的个spring配置文件。***spring***另一种就是:定义一个import.xml文件,通过import将其他多个spring配置文件导入到该文件中,tomcat启动时只需要加载import.xml就可以。IoCDIIoC*********Spring容器(IoC***Spring***Spring(BeanDI***DI(依赖注入***DI***@Autowired?AutowiredBeanPostProcessorIoCDI***注解+XMLSpringApplicationContextcontextnewClassPathXmlApplicationContext("applicationContext.xml");UserServiceservice1=context.getBean(UserService.class);******springJunit给我们***这时,我们需要依靠spring配置文件(注解)******spring-test第二步:通过@RunWithspring***Spring***Spring第三步:通过@ContextConfiguration注解,指定spring运行器需要的配置文件路第四步:通过@AutowiredSpringAOP在软件业,AOP在软件业,AOPAspectOrientedProgramming切面AOP*AOP,制定了一套规范.SpringAOP**AOPOOPSpring*利用AOPAOPAOP(OOP)优化(性能监视、事务管理、安全检查、缓存**重复性代码被抽取出来之后 更加方AOP --所谓连接点是指那些spring2.Pointcut(Joinpoint3.Advice--所谓通知是指Joinpoint4.Introduction(引介)--引介是一种特殊在不修改类代码的前提下,5.Target(-.()AOP类8.Aspect9.Advisor(通知器、顾问AspectAOP实现之AspectJ(了解AspectJAspectJJavaAOPjavaAOP(编译期进行),javaAspectJAOP(当然需要特殊的编译器可以这样说AspectJ是目前实现AOPAspectJ与javajavaAspectJjava(这个过程称为织入),对于织入这个概念,可以简单理解为aspect(切面)应用到目标函数(类)的过程。技术完成JavaJDK(,底层通过反射实现)或者CGLIB的动态ApectJ。ApectJ标类编译时织入,即先编译aspect类再编译目标类。AOP实现之Spring***Spring***SpringAOP技术***而动 是基于反射设计的。(关于反射的知识,请自行学习***技术的实现方式有两种:基于接口的JDK和基于继承的CGLib。JDK使 类来生 对象的一些代码如下使用JDK的方式生 对@authorpublicclass UtilspublicstaticUserService (finalUserServiceservice)//使 类生 对 newInvocationHandler(){throwsThrowable

对象方法一执行,invokepublicObject ,Methodmethod,Object[]//开启事务}

//提交事务//让servicesaveupdatereturnmethod.invoke(service,////返 对 }}CGLib2.编写相关的代码publicstaticUserServiceget//创建CGLIB的Enhancerenhancer=new//设置父类//设置回调函数enhancer.setCallback(newMethodInterceptor(){publicObjectintercept(Objectobj,Methodmethod,Object[]args,Methodmethod)throwsThrowable{//记录日志}returnmethod.invokeSuper(obj,}//生成对UserService=(UserService)return}使用(了解其使用FactoryBean创建使用<aop:advisor>AOPAdvice增强(通知)SpringAspectJAOPSpring+AspectJSpringAspectJSpringAOP******a、开发阶段(我们做的***业务代码(开发主线):***把公用代码抽取出来,制作成通知。(开发阶段最后再做):AOP编程人员来做。切入点与通知间的关系,即切面。:AOP编程人员来做。***b、运行阶段(Spring框架完成的***Spring框架<!--基于<!--基于AspectJ的aop依赖-->编写接口类(目标对象***UserService***UserService实现类springIoC ponent-scanbase-package="sourcecode.ioc"XML编写通知(增强类,一个普通的类springIoCAOP切面execution([**execution:**修饰符:可省略**返回值类型:必须要,但是可以使用***包名:**多级包之间使用.分割**包名可以使用*代替,多级包名可以使用多个*******类名**可以使用***也可以写成**方法名:**也可以使用***也可以写成**参数:**参数使用******配置文件:<aop:beforemethod="beforepointcut-***配置文件:<aop:after-returningmethod="afterReturning"pointcut-***配置文件:<aop:aftermethod="after"pointcut-***配置文件:<aop:aroundmethod="around"pointcut-***配置文件:<aop:after-throwingmethod="afterThrowing"pointcut-编写切面类(注意不是通知类,因为该类中可以指定切入点 ponent-scanbase-AOP用于指定切入点表达式,还可以指定切入点表达式用于指定切入点表达式,还可以指定切入点表达式 ***使用@PointCut***使用@PointCut注解在切面类中定义一个通用的切入点,其他通知可 该切入SpringAOPpublicclassSpringConfiguration}SpringAOP流程(AopNamespaceHandler类Spring应用之SpringJDBCJdbcTemte类的使publicvoidDriverManagerDataSourcedataSource=newDriverManagerDataSource(); te te=new te.update("insertintoaccountvaluesnull,?}Spring管理JdbcTem**步骤一:Spring<propertyname="driverClassName"<propertyname="url"<propertyname="username"<propertyname="password"*步骤二:Spring<bean te" <propertyname="dataSource"*步骤三:编写测试程序publicclassDemo2{private te**先引入DBCP2jar*mons.dbcp-*mons.pool-mavenGAV*编写配置文件<beanid="dataSource" <propertyname="driverClassName"<propertyname="url"value="jdbc:mysql:///spring<propertyname="username"<propertyname="password"*先引入C3P0的jar**编写配置文件<beanid="dataSource"<propertyname="driverClass"<propertyname="jdbcUrl"<propertyname="user"publicvoid te.update("insertintot_accountvalues(null,?}}Spring管理 DBCPC3P0<property<propertyname="password"使用JdbcTemte完成增删改查操publicclassSpringDemo{ privateJdbcTem tejdbcTem //插入操作publicvoid te.update("insertintoaccountvalues(null,?,?)", }//修改操作publicvoid te.update("updateaccountsetname=?,moneywhereid}//删除操作publicvoid te.update("deletefromaccountwhereid=?",}publicvoidAccountaccount=jdbcTem te.queryForObject("select*fromaccountwhereid=?",newBeanMapper(),1);}publicvoidList<Account>list= te.query("select*fromt_account",newfor(Accountaccount:list){for(Accountaccount:list){}}}classBeanMapperimplementspublicAccountmapRow(ResultSetrs,intarg1)throwsSQLException{Accountaccount=newAccount();returnaccount;}}springDAO编写转账案例(包括业务层和持久层DAOJdbcDaoSupportWEBjarIOC6AOP4C3P01MySQLSpringJDBC2JUnit<beanid="dataSource" <propertyname="driverClass"<propertyname="jdbcUrl"value="jdbc:mysql:///spring<propertyname="user"<propertyname="password"SpringSpring<beanid="accountService"<beanid="accountDao"DAODAOJDBC(JdbcDaoSupport<beanid="accountService"<propertyname="accountDao"<beanid="accountDao"<propertyname="dataSource"DAOServicepublicclassAccountDaoImplextendsJdbcDaoSupportimplementsAccountDao{publicvoidoutMoney(Stringout,doublemoney){ te().update("updatet_accountsetmoney=money=?wherename=?",money,out);}publicvoidinMoney(Stringin,doublemoney) te().update("updatet_accountsetmoney=money+?wherename=?",money,in);}}publicclassDemo1{privateAccountServiceaccountService;publicvoid }}Spring原子性一致性*拿转账来说,假设用户A和用户B两者的钱加起来一共是5000,那么不管*性*。*持久性事务并发问题(性导致在事务的并发操作脏读:一个事务到另一个事务未提交的数据以上的结果不一致。update操作果不一致。insert、delete操作事务级为了避免上面出现的几种情况,在标准SQL规范中,定义了4个事务级别,不同的隔四种级别① mitted读未提交)②Readcommitted(读已提交)③Repeatableread可重复读)④Serializable(串行化)默认级大多数数据库的默认级别是Readcommitted,比如Oracle、DB2等MySQL数据库的默认级别是Repeatableread对于多数应用程序,可以优先考虑把数据库系统的级别设为ReadCommitted。它能Spring --平台事务管理器.(真正管理事务的类)Hi

温馨提示

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

评论

0/150

提交评论