【Jsp精品源码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了Jsp精品源码-马士兵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.6distresourcessprin g-beans-2.5.xsd ii. URI: file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resource s/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 e Target f WeaveSpring 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 b Before c AfterReturning d AfterThrowing e After f Around4. 织入点语法 a void void b 参考文档( ..)5. xml 配置 AOP a 把 interceptor 对象初始化 b ltaop:config i. ltaop:aspect ….. 1. ltaop:pointcut 2. ltaop:beforeSpring 整合 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 iv. 在需要事务的方法上加:Transactional v. 需 要 注 意 , 使 用 SessionFactory.getCurrentSession 不 要 使 用 OpenSession c Transactional 详解 i. 什么时候 rollback 1. 运行期异常,非运行期异常不会触发 rollback 2. 必须 uncheck 没有 catch 3. 不管什么异常,只要你 catch 了,spring 就会放弃管理 4. 事务传播特性:propagation_required 5. read_only d xml(推荐,可以同时配置好多方法) i. ltbean txmanager ii. ltaop:config 1. ltaop:pointcut 2. ltaop:advisor pointcut-ref advice-ref iii. lttx:advice: id transaction-manager ltxmlversionquot1.0quotencodingquotUTF‐8quotgtltbeansxmlnsquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema‐instancequot xmlns:contextquothttp://www.springframework.org/schema/contextquot xmlns:aopquothttp://www.springframework.org/schema/aopquot xmlns:txquothttp://www.springframework.org/schema/txquot xsi:schemaLocationquothttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring‐beans‐2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring‐context‐2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring‐aop‐2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring‐tx‐2.5.xsdquotgt ltcontext:annotation‐config/gt ltcontext:component‐scanbase‐packagequotcom.bjsxtquot/gt lt‐‐ ltbeanidquotdataSourcequot classquotorg.apache.commons.dbcp.BasicDataSourcequot destroy‐methodquotclosequotgt ltpropertynamequotdriverClassNamequotvaluequotcom.mysql.jdbc.Driverquot/gt ltpropertynamequoturlquotvaluequotjdbc:mysql://localhost:3306/springquot/gt ltpropertynamequotusernamequotvaluequotrootquot/gt ltpropertynamequotpasswordquotvaluequotbjsxtquot/gt lt/beangt ‐‐gt ltbean classquotorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerquotgt ltpropertynamequotlocationsquotgt ltvaluegtclasspath:jdbc.propertieslt/valuegt lt/propertygt lt/beangt ltbeanidquotdataSourcequotdestroy‐methodquotclosequot classquotorg.apache.commons.dbcp.BasicDataSourcequotgt ltpropertynamequotdriverClassNamequot valuequotjdbc.driverClassNamequot/gt ltpropertynamequoturlquotvaluequotjdbc.urlquot/gt ltpropertynamequotusernamequotvaluequotjdbc.usernamequot/gt ltpropertynamequotpasswordquotvaluequotjdbc.passwordquot/gt lt/beangt ltbeanidquotsessionFactoryquot classquotorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeanquotgt ltpropertynamequotdataSourcequotrefquotdataSourcequot/gt lt‐‐ ltpropertynamequotannotatedClassesquotgt ltlistgt ltvaluegtcom.bjsxt.model.Userlt/valuegt ltvaluegtcom.bjsxt.model.Loglt/valuegt lt/listgt lt/propertygt ‐‐gt ltpropertynamequotpackagesToScanquotgt ltlistgt ltvaluegtcom.bjsxt.modellt/valuegt lt/listgt lt/propertygt ltpropertynamequothibernatePropertiesquotgt ltpropsgt ltpropkeyquothibernate.dialectquotgt org.hibernate.dialect.MySQLDialect lt/propgt ltpropkeyquothibernate.show_sqlquotgttruelt/propgt lt/propsgt lt/propertygt lt/beangt ltbeanidquottxManagerquot classquotorg.springframework.orm.hibernate3.HibernateTransactionManagerquotgt ltpropertynamequotsessionFactoryquotrefquotsessionFactoryquot/gt lt/beangt ltaop:configgt ltaop:pointcutidquotbussinessServicequot expressionquotexecutionpubliccom.bjsxt.service.....quot/gt ltaop:advisorpointcut‐refquotbussinessServicequot advice‐refquottxAdvicequot/gt lt/aop:configgt lttx:adviceidquottxAdvicequottransaction‐managerquottxManagerquotgt lttx:attributesgt lttx:methodnamequotgetUserquotread‐onlyquottruequot/gt lttx:methodnamequotaddquotpropagationquotREQUIREDquot/gt lt/tx:attributesgt lt/tx:advicegtlt/beansgt e HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要) 介绍 i. 设计模式:Template Method ii. Callback:回调/钩子函数 iii. 第一种:(建议) 1. 在 spring 中初始化 HibernateTemplate,注入 sessionFactory 2. DAO 里注入 HibernateTemplate 3. save 写 getHibernateTemplate.save iv. 第二种: 1. 从 HibernateDaoSupport 继承 2. 必须写在 xml 文件中,无法使用 Annotation,因为 set 方法在父类中, 而且是 final 的 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 等 2. 在 beans 配置文件配置对应的实体类,使之受管 vi. 写 dao service 的实现 vii. 加入 Spring 注解 1. 在对应 Service 及 DAO 实现中加入Component,让 spring 对其初 始化 2. 在 Service 上加入Transactional 或者使用 xml 方式 (此处建议后者, 因为更简单) 3. 在 DAO 中注入 sessionFactory 4. 在 Service 中注入 DAO 5. 写 DAO 与 Service 的实现 viii. 写测试 c 整合 Struts2 i. 结合点:Struts2 的 Action 由 Spring 产生 ii. 步骤: 1. 修改 web.xml 加入 struts 的 filter 2. 再加入 spring 的 listener,这样的话,webapp 一旦启动,spring 容器 就初始化了 3. 规划 struts 的 action 和
jsp 展现 4. 加入 struts.xml a 修改配置,由 spring 替代 struts 产生 Action 对象 5. 修改 action 配置 a 把类名改为 bean 对象的名称,这个时候就可以使用首字母小写了 b Scope“prototype”不要忘记 iii. struts 的读常量: 1. struts-default.xml 2. struts-plugin.xml 3. struts.xml 4. struts.properties 5. web.xml iv. 中文问题: 1. Struts2.1.8 已经修正,只需要改 i18n.encoding gbk 2. 使用 spring 的 characterencoding 3. 需要严格注意 filter 的顺序 4. 需要加到 Struts2 的 filter 前面 v. LazyInitializationException 1. OpenSessionInViewFilter 2. 需要严格顺序问题 3. 需要加到 struts2 的 filter 前面ltxmlversionquot1.0quotencodingquotUTF‐8quotgtltweb‐appversionquot2.5quotxmlnsquothttp://java.sun.com/xml/ns/javaeequot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema‐instancequot xsi:schemaLocationquothttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web‐app_2_5.xsdquotgt ltwelcome‐file‐listgt ltwelcome‐filegtindex.jsplt/welcome‐filegt lt/welcome‐file‐listgt ltlistenergt ltlistener‐classgtorg.springframework.web.context.ContextLoaderListenerlt/listener‐classgt lt‐‐default:/WEB‐INF/applicationContext.xml‐‐gt lt/listenergt ltcontext‐paramgt ltparam‐namegtcontextConfigLocationlt/param‐namegt lt‐‐ltparam‐valuegt/WEB‐INF/applicationContext‐.xmlclasspath:applicationContext‐.xmllt/param‐valuegt‐‐gt ltparam‐valuegtclasspath:beans.xmllt/param‐valuegt lt/context‐paramgt ltfiltergt ltfilter‐namegtstruts2lt/filter‐namegt ltfilter‐classgtorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterlt/filter‐classgt lt/filtergt ltfilter‐mappinggt ltfilter‐namegtstruts2lt/filter‐namegt lturl‐patterngt/lt/url‐patterngt lt/filter‐mappinggtlt/web‐appgtltxmlversionquot1.0quotencodingquotUTF‐8quotgtltbeansxmlnsquothttp://www.springframework.org/schema/beansquot xmlns:xsiquothttp://www.w3.org/2001/XMLSchema‐instancequot xmlns:contextquothttp://www.springframework.org/schema/contextquot xmlns:aopquothttp://www.springframework.org/schema/aopquot xmlns:txquothttp://www.springframework.org/schema/txquot xsi:schemaLocationquothttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring‐beans‐2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring‐context‐2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring‐aop‐2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring‐tx‐2.5.xsdquotgt ltcontext:annotation‐config/gt ltcontext:component‐scanbase‐packagequotcom.bjsxtquot/gt lt‐‐ ltbeanidquotdataSourcequot classquotorg.apache.commons.dbcp.BasicDataSourcequot destroy‐methodquotclosequotgt ltpropertynamequotdriverClassNamequotvaluequotcom.mysql.jdbc.Driverquot/gt ltpropertynamequoturlquotvaluequotjdbc:mysql://localhost:3306/springquot/gt ltpropertynamequotusernamequotvaluequotrootquot/gt ltpropertynamequotpasswordquotvaluequotbjsxtquot/gt lt/beangt ‐‐gt ltbean classquotorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerquotgt ltpropertynamequotlocationsquotgt ltvaluegtclasspath:jdbc.propertieslt/valuegt lt/propertygt lt/beangt ltbeanidquotdataSourcequotdestroy‐methodquotclosequot classquotorg.apache.commons.dbcp.BasicDataSourcequotgt ltpropertynamequotdriverClassNamequot valuequotjdbc.driverClassNamequot/gt .