2025-05-23

課程內容

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),有什麼好處

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

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(註解)第一步,導入xml命名空間:

a) 修改xml文件,參考文檔&lt;context:annotation-config />

xmlns:context=http://www.springframework.org/schema/context

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

10. @Autowired

a) 默認按類型by type

b) 如果想用byName,使用@Qulifier

c) 寫在private field(第三種註入形式)(不建議,破壞封裝)

d) 如果寫在set上,@qualifier需要寫在參數上

clip_image002

@Autowired設置springbasedao的屬性sessionfactory

clip_image004

11. @Resource(重要)

a) 加入:j2ee/common-annotations.jar

b) 默認按名稱,名稱找不到,按類型

c) 可以指定特定名稱

d) 推薦使用

e) 不足:如果沒有源碼,就無法運用annotation,隻能使用xml

clip_image006

引用bean

clip_image008

12. @Component @Service @Controller @Repository

a) 初始化的名字默認為類名首字母小寫

b) 可以指定初始化bean的名字

c) 以上集中註解方式暫無區別

設置為“U”

clip_image010

指定註入為“U”

clip_image012

@Controller設置action(規范)

clip_image014

@Service設置service層

clip_image016

@Repository設置dao層

clip_image018

13. @Scope

@Scope("prototype")

在action設置

clip_image020

14. @PostConstruct = init-method; @PreDestroy = destroy-method;

@PreDestroy表對象銷毀前調用此方法

clip_image022

什麼是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

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

a) 把interceptor對象初始化

b) <aop:config

i. &lt;aop:aspect …..

1. &lt;aop:pointcut

2. &lt;aop: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) &lt;bean .. AnnotationSessionFactoryBean>

i. &lt;property dataSource

ii. &lt;annotatedClasses

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. &lt;tx: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. &lt;bean txmanager

ii. &lt;aop:config

1. &lt;aop:pointcut

2. &lt;aop:advisor pointcut-ref advice-ref

iii. &lt;tx:advice: id transaction-manager =

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.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. 在對應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前面

本文出自 “青春行囊” 博客

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *