spring 筆記1

課程內容

  1. 面向接口(抽象)編程的概念與好處
  2. IOC/DI的概念與好處
    1. inversion of control
    2. dependency injection
  3. AOP的概念與好處
  4. Spring簡介
  5. Spring應用IOC/DI(重要)
    1. xml
    2. annotation
  6. Spring應用AOP(重要)
    1. xml
    2. annotation
  7. Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要)
    1. opensessionInviewfilter(記住,解決什麼問題,怎麼解決)
  8. Spring JDBC

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

  1. 場景:用戶添加
  2. Spring_0100_AbstractOrientedProgramming
    1. 不是AOP:Aspect Oriented Programming
  3. 好處:靈活

什麼是IOC(DI),有什麼好處

  1. 把自己new的東西改爲由容器提供
    1. 初始化具體值
    2. 裝配
  2. 好處:靈活裝配

Spring簡介

  1. 項目名稱:Spring_0200_IOC_Introduction
  2. 環境搭建
    1. 只用IOC
      1. spring.jar , jarkata-commons/commons-loggin.jar
  3. IOC容器
    1. 實例化具體bean
    2. 動態裝配
  4. AOP支持
    1. 安全檢查
    2. 管理transaction

Spring IOC配置與應用

  1. FAQ:不給提示:
    1. window – preferences – myeclipse – xml – xml catalog
    2. User Specified Entries – add
      1. Location:    D:\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd
      2. URI:           file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd
      3. Key Type:    Schema Location
      4. Key:        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  2. 注入類型
    1. Spring_0300_IOC_Injection_Type
    2. setter(重要)
    3. 構造方法(可以忘記)
    4. 接口注入(可以忘記)
  3. id vs. name
    1. Spring_0400_IOC_Id_Name
    2. name可以用特殊字符
  4. 簡單屬性的注入
    1. Spring_0500_IOC_SimpleProperty
    2. <property name=… value=….>
  5. <bean 中的scope屬性
    1. Spring_0600_IOC_Bean_Scope
    2. singleton 單例
    3. proptotype 每次創建新的對象
  6. 集合注入
    1. Spring_0700_IOC_Collections
    2. 很少用,不重要!參考程序
  7. 自動裝配
    1. Spring_0800_IOC_AutoWire
    2. byName
    3. byType
    4. 如果所有的bean都用同一種,可以使用beans的屬性:default-autowire
  8. 生命週期
    1. Spring_0900_IOC_Life_Cycle
    2. lazy-init (不重要)
    3. init-method destroy-methd 不要和prototype一起用(瞭解)
  9. Annotation第一步:
    1. 修改xml文件,參考文檔<context:annotation-config />
  10. @Autowired
    1. 默認按類型by type
    2. 如果想用byName,使用@Qulifier
    3. 寫在private field(第三種注入形式)(不建議,破壞封裝)
    4. 如果寫在set上,@qualifier需要寫在參數上
  11. @Resource(重要)
    1. 加入:j2ee/common-annotations.jar
    2. 默認按名稱,名稱找不到,按類型
    3. 可以指定特定名稱
    4. 推薦使用
    5. 不足:如果沒有源碼,就無法運用annotation,只能使用xml
  12. @Component @Service @Controller @Repository
    1. 初始化的名字默認爲類名首字母小寫
    2. 可以指定初始化bean的名字
  13. @Scope
  14. @PostConstruct = init-method; @PreDestroy = destroy-method;

 

什麼是AOP

  1. 面向切面編程Aspect-Oriented-Programming
    1. 是對面向對象的思維方式的有力補充
  2. Spring_1400_AOP_Introduction
  3. 好處:可以動態的添加和刪除在切面上的邏輯而不影響原來的執行代碼
    1. Filter
    2. Struts2的interceptor
  4. 概念:
    1. JoinPoint
    2. PointCut
    3. Aspect(切面)
    4. Advice
    5. Target
    6. Weave

Spring AOP配置與應用

  1. 兩種方式:
    1. 使用Annotation
    2. 使用xml
  2. Annotation
    1. 加上對應的xsd文件spring-aop.xsd
    2. beans.xml <aop:aspectj-autoproxy />
    3. 此時就可以解析對應的Annotation了
    4. 建立我們的攔截類
    5. 用@Aspect註解這個類
    6. 建立處理方法
    7. 用@Before來註解方法
    8. 寫明白切入點(execution …….)
    9. 讓spring對我們的攔截器類進行管理@Component
  3. 常見的Annotation:
    1. @Pointcut
    2. @Before
    3. @AfterReturning
    4. @AfterThrowing
    5. @After
    6. @Around
  4. 織入點語法
    1. void !void
    2. 參考文檔(* ..)
  5. xml配置AOP
    1. 把interceptor對象初始化
    2. <aop:config
      1. <aop:aspect …..
        1. <aop:pointcut
        2. <aop:before

Spring整合Hibernate

Spring 指定datasource

  1. 參考文檔,找dbcp.BasicDataSource
    1. c3p0
    2. dbcp
    3. proxool
  2. 在DAO或者Service中注入dataSource
  3. 在Spring中可以使用PropertyPlaceHolderConfigure來讀取Properties文件的內容

Spring整合Hibernate

  1. <bean .. AnnotationSessionFactoryBean>
    1. <property dataSource
    2. <annotatedClasses
  2. 引入hibernate 系列jar包
  3. User上加Annotation
  4. UserDAO或者UserServie 注入SessionFactory
  5. jar包問題一個一個解決

聲明式的事務管理

  1. 事務加在DAO層還是Service層?
  2. annotation
    1. 加入annotation.xsd
    2. 加入txManager bean
    3. <tx:annotation-driven
    4. 在需要事務的方法上加:@Transactional
    5. 需要注意,使用SessionFactory.getCurrentSession 不要使用OpenSession
  3. @Transactional詳解
    1. 什麼時候rollback
      1. 運行期異常,非運行期異常不會觸發rollback
      2. 必須uncheck (沒有catch)
      3. 不管什麼異常,只要你catch了,spring就會放棄管理
      4. 事務傳播特性:propagation_required
      5. read_only
  4. xml(推薦,可以同時配置好多方法)
    1. <bean txmanager
    2. <aop:config
      1. <aop:pointcut
      2. <aop:advisor pointcut-ref advice-ref
    3. <tx:advice: id transaction-manager =
  5. HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介紹
    1. 設計模式:Template Method
    2. Callback:回調/鉤子函數
    3. 第一種:(建議)
      1. 在spring中初始化HibernateTemplate,注入sessionFactory
      2. DAO裏注入HibernateTemplate
      3. save寫getHibernateTemplate.save();
    4. 第二種:
      1. 從HibernateDaoSupport繼承
      2. 必須寫在xml文件中,無法使用Annotation,因爲set方法在父類中,而且是final的
  6. 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

 

  1. BestPractice
    1. 將這些所有的jar包保存到一個位置,使用的時候直接copy
  2. 步驟
    1. 加入jar
    2. 首先整合Spring + Hibernate
      1. 建立對應的package
        1. dao / dao.impl / model / service / service.impl/ test
      2. 建立對應的接口與類框架
        1. S2SH_01
      3. 建立spring的配置文件(建議自己保留一份經常使用的配置文件,以後用到的時候直接copy改)
      4. 建立數據庫
      5. 加入Hibernate註解
        1. 在實體類上加相應註解@Entity @Id
        2. beans配置文件配置對應的實體類,使之受管
      6. dao service的實現
      7. 加入Spring註解
        1. 在對應ServiceDAO實現中加入@Component,讓spring對其初始化
        2. Service上加入@Transactional或者使用xml方式(此處建議後者,因爲更簡單)
        3. DAO中注入sessionFactory
        4. Service中注入DAO
        5. DAOService的實現
      8. 寫測試
    3. 整合Struts2
      1. 結合點:Struts2ActionSpring產生
      2. 步驟:
        1. 修改web.xml加入 strutsfilter
        2. 再加入springlistener,這樣的話,webapp一旦啓動,spring容器就初始化了
        3. 規劃strutsactionjsp展現
        4. 加入struts.xml
          1. 修改配置,由spring替代struts產生Action對象
        5. 修改action配置
          1. 把類名改爲bean對象的名稱,這個時候就可以使用首字母小寫了
          2. @Scope(prototype)不要忘記
      3. struts的讀常量:
        1. struts-default.xml
        2. struts-plugin.xml
        3. struts.xml
        4. struts.properties
        5. web.xml
      4. 中文問題:
        1. Struts2.1.8已經修正,只需要改i18n.encoding = gbk
        2. 使用springcharacterencoding
        3. 需要嚴格注意filter的順序
        4. 需要加到Struts2filter前面
      5. LazyInitializationException
        1. OpenSessionInViewFilter
        2. 需要嚴格順序問題
        3. 需要加到struts2filter前面
  1. Spring TestContext 測試框架

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章