马士兵Spring学习笔记.docx
- 文档编号:7092259
- 上传时间:2023-01-17
- 格式:DOCX
- 页数:18
- 大小:23.06KB
马士兵Spring学习笔记.docx
《马士兵Spring学习笔记.docx》由会员分享,可在线阅读,更多相关《马士兵Spring学习笔记.docx(18页珍藏版)》请在冰豆网上搜索。
马士兵Spring学习笔记
课程内容
1.面向接口(抽象)编程的概念与好处
2.IOC/DI的概念与好处
a)inversionofcontrol
b)dependencyinjection
3.AOP的概念与好处
4.Spring简介
5.Spring应用IOC/DI(重要)
a)xml
b)annotation
6.Spring应用AOP(重要)
a)xml
b)annotation
7.Struts2.1.6+Spring2.5.6+Hibernate3.3.2整合(重要)
a)opensessionInviewfilter(记住,解决什么问题,怎么解决)
8.SpringJDBC
面向接口编程(面向抽象编程)
1.场景:
用户添加
2.Spring_0100_AbstractOrientedProgramming
a)不是AOP:
AspectOrientedProgramming
3.好处:
灵活
什么是IOC(DI),有什么好处
1.把自己new的东西改为由容器提供
a)初始化具体值
b)装配
2.好处:
灵活装配
Spring简介
1.项目名称:
Spring_0200_IOC_Introduction
2.环境搭建
a)只用IOC
i.spring.jar,jarkata-commons/commons-loggin.jar
3.IOC容器
a)实例化具体bean
b)动态装配
4.AOP支持
a)安全检查
b)管理transaction
SpringIOC配置与应用
1.FAQ:
不给提示:
a)window–preferences–myeclipse–xml–xmlcatalog
b)UserSpecifiedEntries–add
i.Location:
D:
\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-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.KeyType:
SchemaLocation
iv.Key:
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
2.注入类型
a)Spring_0300_IOC_Injection_Type
b)setter(重要)
c)构造方法(可以忘记)
d)接口注入(可以忘记)
3.idvs.name
a)Spring_0400_IOC_Id_Name
b)name可以用特殊字符
4.简单属性的注入
a)Spring_0500_IOC_SimpleProperty
b)
5. 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-autowire 8.生命周期 a)Spring_0900_IOC_Life_Cycle b)lazy-init(不重要) c)init-methoddestroy-methd不要和prototype一起用(了解) 9.Annotation第一步: a)修改xml文件,参考文档 annotation-config/> 10.@Autowired a)默认按类型bytype b)如果想用byName,使用@Qulifier c)写在privatefield(第三种注入形式)(不建议,破坏封装) d)如果写在set上,@qualifier需要写在参数上 11.@Resource(重要) a)加入: j2ee/common-annotations.jar b)默认按名称,名称找不到,按类型 c)可以指定特定名称 d)推荐使用 e)不足: 如果没有源码,就无法运用annotation,只能使用xml 12.@Component@Service@Controller@Repository a)初始化的名字默认为类名首字母小写 b)可以指定初始化bean的名字 13.@Scope 14.@PostConstruct=init-method;@PreDestroy=destroy-method; 什么是AOP 1.面向切面编程Aspect-Oriented-Programming a)是对面向对象的思维方式的有力补充 2.Spring_1400_AOP_Introduction 3.好处: 可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码 a)Filter b)Struts2的interceptor 4.概念: a)JoinPoint b)PointCut c)Aspect(切面) d)Advice e)Target f)Weave SpringAOP配置与应用 1.两种方式: a)使用Annotation b)使用xml 2.Annotation a)加上对应的xsd文件spring-aop.xsd b)beans.xml aspectj-autoproxy/> c)此时就可以解析对应的Annotation了 d)建立我们的拦截类 e)用@Aspect注解这个类 f)建立处理方法 g)用@Before来注解方法 h)写明白切入点(execution…….) i)让spring对我们的拦截器类进行管理@Component 3.常见的Annotation: a)@Pointcut b)@Before c)@AfterReturning d)@AfterThrowing e)@After f)@Around 4.织入点语法 a)void! void b)参考文档(*..) 5.xml配置AOP a)把interceptor对象初始化 b) config i. aspect….. 1. pointcut 2. before Spring整合Hibernate 1.Spring指定datasource a)参考文档,找dbcp.BasicDataSource i.c3p0 ii.dbcp iii.proxool b)在DAO或者Service中注入dataSource c)在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容 2.Spring整合Hibernate a) i. ii. b)引入hibernate系列jar包 c)User上加Annotation d)UserDAO或者UserServie注入SessionFactory e)jar包问题一个一个解决 3.声明式的事务管理 a)事务加在DAO层还是Service层? b)annotation i.加入annotation.xsd ii.加入txManagerbean iii. 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. ii. config 1. pointcut 2. advisorpointcut-refadvice-ref iii. advice: idtransaction-manager= xmlversion="1.0"encoding="UTF-8"? > //www.springframework.org/schema/beans" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xmlns: context="http: //www.springframework.org/schema/context" xmlns: aop="http: //www.springframework.org/schema/aop" xmlns: tx="http: //www.springframework.org/schema/tx" xsi: schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd http: //www.springframework.org/schema/contexthttp: //www.springframework.org/schema/context/spring-context-2.5.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-2.5.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-2.5.xsd"> annotation-config/> component-scanbase-package="com.bjsxt"/> -- class="mons.dbcp.BasicDataSource" destroy-method="close"> mysql: //localhost: 3306/spring"/>
-->
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> jdbc.properties
class="mons.dbcp.BasicDataSource"> value="${jdbc.driverClassName}"/> class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> -- --> org.hibernate.dialect.MySQLDialect class="org.springframework.orm.hibernate3.HibernateTransactionManager"> config> pointcutid="bussinessService" expression="execution(public*com.bjsxt.service..*.*(..))"/> advisorpointcut-ref="bussinessService" advice-ref="txAdvice"/> config> adviceid="txAdvice"transaction-manager="txManager"> attributes> methodname="getUser"read-only="true"/> methodname="add*"propagation="REQUIRED"/> attributes> advice> e)HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍 i.设计模式: TemplateMethod 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.2 1.需要的jar包列表 jar包名称 所在位置 说明 antlr-2.7.6.jar hibernate/lib/required 解析HQL aspectjrt spring/lib/aspectj AOP aspectjweaver .. AOP cglib-nodep-2.1_3.jar spring/lib/cglib 代理,二进制增强 common-annotations.jar spring/lib/j2ee @Resource commons-collections-3.1.jar hibernate/lib/required 集合框架 commons-fileupload-1.2.1.jar struts/lib struts commons-io-1.3.2 struts/lib struts commons-logging-1.1.1 单独下载,删除1.0.4(struts/lib) struts spring dom4j-1.6.1.jar hibernate/required 解析xml ejb3-persistence hibernate-annotation/lib @Entity freemarker-2.3.13 struts/lib struts hibernate3.jar hibernate hibernate-annotations hibernate-annotation/ hibernate-common-annotations hibernate-annotation/lib javassist-3.9.0.GA.jar hiberante/lib/required hibernate jta-1.1.jar .. hibernatetransaction junit4.5 mysql- ognl-2.6.11.jar struts/lib slf4j-api-1.5.8.jar hibernate/lib/required hibernate-log slf4j-nop-1.5.8.jar hibernate/lib/required spring.jar spring/dist struts2-core-2.1.6.jar struts/lib xwork-2.1.2.jar struts/lib struts2 commons-dbcp spring/lib/jarkata-commons commons-pool.jar .. struts2-spring-plugin-2.1.6.jar struts/lib 2.BestPractice: a)将这些所有的jar包保存到一个位置,使用的时候直接copy 3.步骤 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.写daoservice的实现 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前面 xmlversion="1.0"encoding="UTF-8"? > //jav
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 士兵 Spring 学习 笔记