【Jsp精品源码栏目提醒】:本文主要为网学会员提供“Spring学习笔记(马士兵spring视频笔记) - 其它资料”,希望对需要Spring学习笔记(马士兵spring视频笔记) - 其它资料网友有所帮助,学习一下!
课程内容1. 面向接口(抽象)编程的概念与好处2. IOC/DI 的概念与好处 a inversion of control b dependency injection3. AOP 的概念与好处4. Spring 简介5. Spring 应用 IOC/DI(重要) a xml b annotation6. Spring 应用 AOP(重要) a xml b annotation7. Struts2.1.6 Spring2.5.6 Hibernate3.3.2 整合(重要) a opensessionInviewfilter(记住,解决什么问题,怎么解决)8. Spring JDBC面向接口编程(面向抽象编程)1. 场景:用户添加2. Spring_0100_AbstractOrientedProgramming a 不是 AOP:Aspect Oriented Programming3. 好处:灵活什么是 IOC(DI),有什么好处1. 把自己 new 的东西改为由容器提供 a 初始化具体值 b 装配2. 好处:灵活装配Spring 简介1. 项目名称:Spring_0200_IOC_Introduction2. 环境搭建 a 只用 IOC i. spring.jar jarkata-commons/commons-loggin.jar3. IOC 容器 a 实例化具体 bean b 动态装配4. AOP 支持 a 安全检查 b 管理 transactionSpring IOC 配置与应用1. FAQ:不给提示: a window – preferences – myeclipse – xml – xml catalog b User Specified Entries – add i. Location:D:share0900_Springsoftspring-framework-2.5.6distresourcesspring-beans-2.5.xsd ii. URI: file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd iii. Key Type: Schema Location iv. Key: http://www.springframework.org/schema/beans/spring-beans-2.5.xsd2. 注入类型 a Spring_0300_IOC_Injection_Type b setter(重要) c 构造方法(可以忘记) d 接口注入(可以忘记)3. id vs. name a Spring_0400_IOC_Id_Name b name 可以用特殊字符4. 简单属性的注入 a Spring_0500_IOC_SimpleProperty b ltproperty name… value….gt5. ltbean 中的 scope 属性 a Spring_0600_IOC_Bean_Scope b singleton 单例 c proptotype 每次创建新的对象6. 集合注入 a Spring_0700_IOC_Collections b 很少用,不重要!参考程序7. 自动装配 a Spring_0800_IOC_AutoWire b byName c byType d 如果所有的 bean 都用同一种,可以使用 beans 的属性:default-autowire8. 生命周期 a Spring_0900_IOC_Life_Cycle b lazy-init 不重要 c init-method destroy-methd 不要和 prototype 一起用(了解)9. Annotation 第一步: a 修改 xml 文件,参考文档ltcontext:annotation-config /gt10. Autowired a 默认按类型 by type b 如果想用 byName,使用Qulifier c 写在 private field(第三种注入形式)(不建议,破坏封装) d 如果写在 set 上,qualifier 需要写在参数上11. Resource(重要) a 加入:j2ee/common-annotations.jar b 默认按名称,名称找不到,按类型 c 可以指定特定名称 d 推荐使用 e 不足:如果没有源码,就无法运用 annotation,只能使用 xml12. Component Service Controller Repository a 初始化的名字默认为类名首字母小写 b 可以指定初始化 bean 的名字13. Scope14. PostConstruct init-method PreDestroy destroy-method什么是 AOP1. 面向切面编程 Aspect-Oriented-Programming a 是对面向对象的思维方式的有力补充2. Spring_1400_AOP_Introduction3. 好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码 a Filter b Struts2 的 interceptor4. 概念: a JoinPoint 释意:切面与原方法交接点 即 切入点 b PointCut 释意:切入点集合 c Aspect(切面)释意:可理解为代理类前说明 d Advice 释意:可理解为代理方法前说明 例如Before e Target 释意:被代理对象 被织入对象 f Weave 释意:织入Spring AOP 配置与应用1. 两种方式: a 使用 Annotation b 使用 xml2. Annotation a 加上对应的 xsd 文件 spring-aop.xsd b beans.xml ltaop:aspectj-autoproxy /gt c 此时就可以解析对应的 Annotation 了 d 建立我们的拦截类 e 用Aspect 注解这个类 f 建立处理方法 g 用Before 来注解方法 h 写明白切入点(execution …….) i 让 spring 对我们的拦截器类进行管理Component3. 常见的 Annotation: a Pointcut 切入点声明 以供其他方法使用 例子如下: Aspect Component public class LogInterceptor Pointcutquotexecutionpublic com.bjsxt.dao.....quot public void myMethod AroundquotmyMethodquot public void beforeProceedingJoinPoint pjp throws Throwable System.out.printlnquotmethod beforequot pjp.proceed AfterReturningquotmyMethodquot public void afterReturning throws Throwable System.out.printlnquotmethod afterReturningquot AfterquotmyMethodquot public void afterFinily throws Throwable System.out.printlnquotmethod endquot b Before 发放执行之前织入 c AfterReturning 方法正常执行完返回之后织入无异常 d AfterThrowing 方法抛出异常后织入 e After 类似异常的 finally f Around 环绕 类似 filter 如需继续往下执行则需要像 filter 中执行 FilterChain.doFilter..对象一样 执行 ProceedingJoinPoint.proceed方可例子如下: Aroundquotexecution com.bjsxt.dao.....quot public void beforeProceedingJoinPoint pjp throws Throwable System.out.printlnquotmethod startquot pjp.proceed//类似FilterChain.doFilter..告诉jvm继续向下执行 4. 织入点语法 a void void b 参考文档( ..) 如果 execution com.bjsxt.dao.....中声明的方法不是接口实现 则无法使用 AOP 实现动态代理此时 可引入包” cglib-nodep-2.1_3.jar” 后有 spring 自动将普通类在 jvm 中编译为接口实现类从而打到可正常使 用 AOP 的目的.5. xml 配置 AOP a 把 interceptor 对象初始化 b ltaop:config i. ltaop:aspect ….. 1. ltaop:pointcut 2. ltaop:before 例子:ltbean idquotlogInterceptorquot classquotcom.bjsxt.aop.LogInterceptorquotgtlt/beangt ltaop:configgt lt-- 配置一个切面 --gt ltaop:aspect idquotpointquot refquotlogInterceptorquotgt lt-- 配置切入点,指定切入点表达式 --gt lt-- 此句也可放到 aop:aspect标签外 依然有效--gt ltaop:pointcut expression quotexecutionpublic com.bjsxt.service.....quot idquotmyMethodquot /gt lt-- 应用前置通知 --gt ltaop:before methodquotbeforequot pointcut-refquotmyMethodquot /gt lt-- 应用环绕通知 需指定向下进行 --gt ltaop:around methodquotaroundquot pointcut-refquotmyMethodquot /gt lt-- 应用后通知 --gt ltaop:after-returning methodquotafterReturningquot pointcut-refquotmyMethodquot /gt lt-- 应用抛出异常后通知 --gt ltaop:after-throwing methodquotafterThrowingquot pointcut-refquotmyMethodquot /gt lt-- 应用最终通知 --gt ltaop:after methodquotafterFinilyquot pointcutquotexecutionpublic om.bjsxt.service.....quot /gt lt/aop:aspectgt lt/aop:configgtSpring 整合 Hibernate1. Spring 指定 datasource a 参考文档,找 dbcp.BasicDataSource i. c3p0 ii. dbcp iii. proxool b 在 DAO 或者 Service 中注入 dataSource c 在 Spring 中可以使用 PropertyPlaceHolderConfigure 来读取 Properties 文件的内容2. Spring 整合 Hibernate a ltbean .. AnnotationSessionFactoryBeangt i. ltproperty dataSource ii. ltannotatedClasses b 引入 hibernate 系列 jar 包 c User 上加 Annotation d UserDAO 或者 UserServie 注入 SessionFactory e jar 包问题一个一个解决3. 声明式的事务管理 a 事务加在 DAO 层还是 Service 层? b annotation i. 加入 annotation.xsd ii. 加入 txManager bean iii. lttx:annotation-driven 例如: ltbean idquottransactionManagerquot classquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt ltproperty namequotsessionFactoryquotgt ltref beanquotsessionFactoryquot /gt lt/propertygt lt/beangt lttx:annotation-driven transaction-managerquottransactionManagerquot/gt iv. 在需要事务的方法上加:Transactional v. 需 要 注 意 , Hibernate 获 得 session 时 要 使 用 SessionFactory.getCurrentSession 不 能 使 用 OpenSession c Transactional 详解 i. 什么时候 rollback 1. 运行期异常,非运行期异常不会触发 rollback 2. 必须 uncheck 没有 catch 3. 不管什么异常,只要你 catch 了,spring 就会放弃管理 4. 事务传播特性:propagation_required 例如: TransactionalpropagationPropagation.REQUIRED等同于Transactional 作用一个方法声明了Transactional 事务后其内再调用的方法不需要再声明Transactional. 5. read_only 例如: TransactionalpropagationPropagation.REQUIREDreadOnlytrue 当方法声明 readOnlytrue 时该方法及其调用的方法内都不执行 insert update 等 d xml(推荐,可以同时配置好多方法) i. ltbean txmanager ii. ltaop:config 1. ltaop:pointcut 2. ltaop:advisor pointcut-ref advice-ref iii. lttx:advice: id transaction-manager iv. ltproperty namequotpackagesToScanquotgt 可定义扫描目标包下所有实体类 例如: ltbean idquotsessionFactoryquot classquotorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactor yBeanquotgt ltproperty namequotdataSourcequot refquotdataSourcequot /gt ltproperty namequothibernatePropertiesquotgt ltpropsgt ltprop keyquothibernate.dialectquotgtorg.hibernate.dialect.OracleDialectlt/propgt ltprop keyquothibernate.show_sqlquotgttruelt/propgt lt/propsgt lt/propertygt lt-- ltproperty namequotannotatedClassesquotgt ltlistgt ltvaluegtcom.bjsxt.model.TestUserlt/valuegt ltvaluegtcom.bjsxt.model.Loglt/valuegt lt/listgt lt/propertygt --gt lt-- 将参数名称设为 packagesToScan 可定义扫描目标包下所有实体类 --gt ltproperty namequotpackagesToScanquotgt ltlistgt ltvaluegtcom.bjsxt.modellt/valuegt lt/listgt lt/propertygtlt/beangt//事务管理transactional 的注入ltbean idquottransactionManagerquot classquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt ltproperty namequotsessionFactoryquotgt ltref beanquotsessionFactoryquot /gt lt/propertygtlt/beangt//定义字符串“execution() ”为一个变量名,简化ltaop:configgt ltaop:pointcut expressionquotexecutionpublic com.bjsxt.service.....quot idquotmyServiceMethodquot /gt ltaop:advisor pointcut-refquotmyServiceMethodquot advice-refquottxAdvicequot/gtlt/aop:configgtlttx:advice idquottxAdvicequot transaction-managerquottransactionManagerquotgt lttx:attributesgt lttx:method namequotsavequot propagationquotREQUIREDquot /gt lttx:method namequotaddquot propagationquotREQUIREDquot /gt lttx:method namequotupdatequot propagationquotREQUIREDquot /gt lttx:method namequotdelquot propagationquotREQUIREDquot /gt lttx:method namequotcancelquot propagationquotREQUIREDquot /gt lttx:method namequotquot read-onlyquottruequot /gt lt/tx:attributesgtlt/tx:advicegte HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍 i. 设计模式:Template Method模板方法 ii. Callback:回调/钩子函数 iii. 第一种:(建议) 1. 在 spring 中初始化 HibernateTemplate,注入 sessionFactory ltbean idquothibernateTemplatequot classquotorg.springframework.orm.hibernate3.HibernateTemplatequotgt ltproperty namequotsessionFactoryquot refquotsessionFactoryquot /gt lt/beangt 2. DAO 里注入 HibernateTemplate private HibernateTemplate hibernateTemplate Resource public void setHibernateTemplateHibernateTemplate hibernateTemplate this.hibernateTemplate hibernateTemplate 3. save 写 getHibernateTemplate.save public void saveTestUser testUser hibernateTemplate.savetestUser iv. 第二种: 1. 从 HibernateDaoSupport 继承此方法不好用 可忽略 2. 必须写在 xml 文件中,无法使用 Annotation,因为 set 方法在父类中,而且是 final 的 例如: 首先新建 SuperDAOImpl 类使用 Annotation 注入--Component: Component public class SuperDAOImpl private HibernateTemplate hibernateTemplate //此处定义由 spring 注入管理 public HibernateTemplate getHibernateTemplate return hibernateTemplate Resource public void setHibernateTemplateHibernateTemplate hibernateTemplate this.hibernateTemplate hibernateTemplate 此时xml 中必须要有: ltbean idquothibernateTemplatequot classquotorg.springframework.orm.hibernate3.HibernateTemplatequotgt ltproperty namequotsessionFactoryquot refquotsessionFactoryquot /gt lt/beangt 或者SuperDAOImpl 类写成下面代码: Component public class SuperDAOImpl extends HibernateDaoSupport ResourcenamequotsessionFactoryquot public void setSuperHibernateTemplateSessionFactory sessionFactory super.setSessionFactorysessionFactory 对应的 xml 中则可省略 ltbean idquothibernateTemplatequot………部分 只要包含 ltbean idquotsessionFactoryquot……..部分即可 最后其他类继承 SuperDaoImpl 类后便可直接使用 HibernateTemplate Componentquotuquot public class UserDAOImpl extends SuperDAOImpl implements UserDAO public void saveTestUser testUser this.getHibernateTemplate.savetestUser f spring 整合 hibernate 的时候使用 packagesToScan 属性,可以让 spring 自动扫描对应包下面的实体类Struts2.1.6 Spring2.5.6 Hibernate3.3.21. 需要的 jar 包列表jar 包名称 所在位置 说明antlr-2.7.6.jar hibernate/lib/required 解析 HQLaspectjrt spring/lib/aspectj AOPaspectjweaver .. AOPcglib-nodep-2.1_3.jar spring/lib/cglib 代理,二进制增强common-annotations.jar spring/lib/j2ee Resourcecommons-collections-3.1.jar hibernate/lib/required 集合框架commons-fileupload-1.2.1.jar struts/lib strutscommons-io-1.3.2 struts/lib strutscommons-logging-1.1.1 单 独 下 载 , 删 除 struts 1.0.4struts/lib springdom4j-1.6.1.jar hibernate/required 解析 xmlejb3-persistence hibernate-annotation/lib Entityfreemarker-2.3.13 struts/lib strutshibernate3.jar hibernatehibernate-annotations hibernate-annotation/hibernate-common-annotations hibernate-annotation/libjavassist-3.9.0.GA.jar hiberante/lib/required hibernatejta-1.1.jar .. hibernate transactionjunit4.5mysql-ognl-2.6.11.jar struts/libslf4j-api-1.5.8.jar hibernate/lib/required hibernate-logslf4j-nop-1.5.8.jar hibernate/lib/requiredspring.jar spring/diststruts2-core-2.1.6.jar struts/libxwork-2.1.2.jar struts/lib struts2commons-dbcp spring/lib/jarkata-commonscommons-pool.jar ..struts2-spring-plugin-2.1.6.jar struts/lib2. BestPractice: a 将这些所有的 jar 包保存到一个位置,使用的时候直接 copy3. 步骤 a 加入 jar 包 b 首先整合 Spring Hibernate i. 建立对应的 package 1. dao / dao.impl / model / service / service.impl/ test ii. 建立对应的接口与类框架 1. S2SH_01 iii. 建立 spring 的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接 copy 改) iv. 建立数据库 v. 加入 Hibernate 注解 1. 在实体类上加相应注解Entity Id 等 在字段属性的 get 方法上加--Columnname quot表字段名quot 2. 在 beans 配置文件配置对应的实体类,使之受管 vi. 写 dao service 的实现 vii. 加入 Spring 注解 1. 在对应 Service 及 DAO 实现中加入Component,让 spring 对其初始化 2. 在 Service 上.