Spring笔记

来源:互联网 发布:grapher软件绘图教学 编辑:程序博客网 时间:2024/04/26 21:01

课程内容

1. 面向接口(抽象)编程的概念与好处

2. IOC/DI的概念与好处

a) inversion of control

b) dependency injection

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. Spring JDBC

面向接口编程(面向抽象编程)

1. 场景:用户添加

2. Spring_0100_AbstractOrientedProgramming

a) 不是AOP:Aspect Oriented Programming

3. 好处:灵活

什么是IOC(控制翻转)DI),有什么好处Dependency Injection(依赖注入)

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) 动态把其它的bean装到其中一个bean

4. AOP支持

a) 安全检查

b) 管理transaction

Spring IOC配置与应用

1. FAQ:不给提示:

a) window – preferences – myeclipse – xml – xml catalog

b) User Specified Entries – 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. Key Type: Schema Location

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. id vs. name

a) Spring_0400_IOC_Id_Name

b) name可以用特殊字符

4. 简单属性的注入

a) Spring_0500_IOC_SimpleProperty

b) <property name=… value=….>

5. <bean 中的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-autowire

8. 生命周期

a) Spring_0900_IOC_Life_Cycle

b) lazy-init (不重要)

c) init-method destroy-methd 不要和prototype一起用(了解)

9. Annotation第一步:

a) 修改xml文件,参考文档<context:annotation-config />

10. @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,只能使用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) Struts2interceptor

4. 概念:

a) JoinPoint  连接点

b) PointCut  连接点的集合  

c) Aspect(切面)

d) Advice  切面的逻辑

e) Target  被代理对象

f) Weave 织入

Spring AOP配置与应用

1. 两种方式:

a) 使用Annotation

b) 使用xml

2. Annotation

a) 加上对应的xsd文件spring-aop.xsd

b) beans.xml <aop: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 用别人的切面类逻辑的时候只能用xml

a) 把interceptor对象初始化

b) <aop:config

i. <aop:aspect …..

1. <aop:pointcut

2. <aop:before

Spring整合Hibernate

1. Spring 指定datasource

a) 参考文档,找dbcp.BasicDataSource  database connection pool

i. c3p0

ii. dbcp

iii. proxool

b) 在DAO或者Service中注入dataSource

c) 在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容

2. Spring整合Hibernate 

a) <bean .. AnnotationSessionFactoryBean>

i. <property dataSource

ii. <annotatedClasses

iii. <property namehibernateproperties> <props> <prop key=hibernate.dialect></prop> </props> </property>

b) 引入hibernate 系列jar

c) User上加Annotation

d) UserDAO或者UserServie 注入SessionFactory

e) jar包问题一个一个解决

3. 声明式的事务管理

a) 事务加在DAO层还是Service层?

b) annotation

i. 加入annotation.xsd

ii. 加入txManager bean 把sessionfactory注入给它

iii. <tx:annotation-driven

iv. 在需要事务的方法上加:@Transactional

v. 需要注意,使用SessionFactory.getCurrentSession 不要使用OpenSession

c) @Transactional详解

i. 什么时候rollback 

1. 运行期异常,非运行期异常不会触发rollback

2. 必须uncheck (没有catch)

3. 不管什么异常,只要你catch了,spring就会放弃管理

4. 事务传播特性:propagation_required  默认值required别人调我的方法,如果说已经有了一个transaction了,就用原来那个,把自己作为这个transaction的一部分加进去,如果没有,就创建一个新的。

5. read_only readonlytransaction比其它的效率高,提高性能的

d) xml推荐,可以同时配置好多方法

i. <bean txmanager

ii. <aop:config 

1. <aop:pointcut

2. <aop:advisor pointcut-ref advice-ref

iii. <tx:advice: id transaction-manager = 

e) HibernateTemplateHibernateCallbackHibernateDaoSupport(不重要)介绍

i. 设计模式:Template Method

ii. Callback:回调/钩子函数

iii. 第一种:(建议)

1. 在spring中初始化HibernateTemplate,注入sessionFactory

2. DAO里注入HibernateTemplate

3. savegetHibernateTemplate.save(); 自己写个surperdao 在它

里面把hibernateTemplate注入进来,hibernatesupport不需要了

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

..

hibernate transaction

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. 写dao service的实现

vii. 加入Spring注解

1. 在对应ServiceDAO实现中加入@Component,让spring对其初始化

2. 在Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)

3. 在DAO中注入sessionFactory

4. 在Service中注入DAO

5. 写DAOService的实现

viii. 写测试

c) 整合Struts2

结合点:Struts2ActionSpring产生需要在struts.xml class=“名字” 否则action完全由struts spring plugin插件完成 产生以后它会去到spring那边容器找需要注入的内容。注意用spring产生的话千万不要忘了@prototype

i. 

ii. 步骤:

1. 修改web.xml加入 strutsfilter

2. 再加入springlistener,这样的话,webapp一旦启动,spring容器就初始化了

3. 规划strutsactionjsp展现

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. 使用springcharacterencoding

3. 需要严格注意filter的顺序

4. 需要加到Struts2filter前面

v. LazyInitializationException

1. OpenSessionInViewFilter

2. 需要严格顺序问题

3. 需要加到struts2filter前面

Action的产生完全由struts spring plugin插件完成 产生以后它会去到spring那边容器找需要注入的内容。

Struts default properties

学会spring下面的自动化测试框架 springtestcontext 

opensessioninviewinterceptor

原创粉丝点击