框架應用 : Spring - 開發詳述

Spring framework簡介

  spring framework這個框架是spring項目中的核心項目,全部框架都依賴於這個框架.java

  它是一個一站式的開源框架,基礎技術是IoC.mysql

  

  按官方文檔主要分塊爲核心技術,測試,數據訪問,Web,Integrationweb

  核心技術主要表如今Ioc容器,資源訪問類Resource,校驗,數據綁定和類型轉換,Spring EL表達式,AOPredis

  測試包含單元測試以及Integration測試spring

  數據訪問包含事務管理, DAO support, JDBC支持, ORM支持(Mybatis,Hibernate)sql

  Web包含Spring MVC,WebSocket支持,其餘MVC框架支持(Struts)數據庫

  Integration是分佈式項目會使用到的功能,不瞭解.express

  

  上圖的每個模塊都對應了至關的jar包,因此使用某一模塊必須把jar包依賴導入,編程

IoC概念以及目標

  

  IoC就是讓本來你本身管理的對象交由容器來進行管理,其主要的目的是鬆耦合.設計模式

IoC發展史

  既然IoC的目標是爲了鬆耦合,那它怎麼作到的?

  

  最後目標:下降對象之間的耦合度,IoC技術加入了配置把編碼中對象的耦合度下降了.

IoC的底層原理

  IoC底層使用的技術包括:

     (1)xml配置文件

     (2)dom4j解決xml

     (3)工廠設計模式

     (4)反射  

        

IoC應用入門

  一.導入jar包

    IoC是Spring framework的基礎技術,因此須要導入基礎包;

    

  二.建立類,在類裏面建立方法

    須要建立交與容器的類模板(擁有getter和setter的POJO類)

    

  三.建立spring配置文件,配置建立類

    (1)spring的核心配置文件名稱和位置不是固定的,

    官方推薦放置於src下面,命名爲applicationContext.xml

    (2)引入schema約束

    

    

    (3)配置對象建立

    

  四.測試對象建立

      

xml配置文件頭部提示錯誤

  解決方法是把schema約束引入spring,把約束文件引入到spring中.

   

   

 

 

IoC的三種配置管理方式

  xml配置

    實例化的三種方式

      第一種使用類無參構造(重點)

        

      第二種使用靜態工廠來進行實例化

      

      

      第三種使用實例工廠建立

      

      

  bean標籤經常使用的屬性

     經常使用屬性有id, class, name, scope

     id用於建立標識

     class鍵入類的全路徑名,引入模板類

     name和id功能一致,但name容許包含特殊字符

     scope用於指定建立類的方式以及其使用範圍,參數以下

       -singleton  單例建立對象,也就是始終都複用同一個對象,不會進行第二輪的建立

       -prototype  每次建立都會建立一個新的對象

       -request   建立對象並放置到request域中

       -session  建立對象並放置到session域中

       -globalSession  用於實現單點登陸功能,好比百度下有百度雲,百度翻譯,百度相冊之類多個應用,可是你只要登陸上一個位置,多個位置均可以使用登陸信息,這就是單點登陸;這個參數基本不會使用,由於有一種就redis的技術更好地實現了這種功能.

 

  屬性注入

    屬性注入三種方式

      

  set方法注入屬性,其中包含基礎屬性注入,對象屬性注入,複合屬性注入

    基礎屬性注入

      

       

    對象屬性注入

      

      

    複合屬性的注入

      1.數組

      2.list集合

      3.map集合

      4.properties

<bean id="person" class="com.harry.ioc.test">
        <!-- 數組 -->
        <property name="arrs">
            <list>
                <value>引用名1</value>
                <value>引用名2</value>
                <value>引用名3</value>
            </list>
        </property>
        
        <!-- list -->
        <property name="list">
            <list>
                <value>引用名4</value>
                <value>引用名5</value>
                <value>引用名6</value>
            </list>            
        </property>
        
        <!-- map -->
        <property name="map">
            <map>
                <entry key="aa" value="引用名7"></entry>
                <entry key="bb" value="引用名8"></entry>
                <entry key="cc" value="引用名9"></entry>
            </map>
        </property>
        
        <!-- properties -->
        <property name="properties">
            <props>
                <prop key="driverclass">com.mysql.jdbc.Driver</prop>
                <prop key="username">root</prop>
            </props>
        </property>
    </bean>
複合屬性注入

  有參構造函數注入屬性

      

  javaConfig類(有待補充)

  註解

    一.註解介紹

      1.代碼裏特殊標記,使用註解能夠完成功能

      2.註解寫法 @(屬性名稱=屬性值)

      3.註解可使用在類,方法和屬性之上

    二.spring的註解開發導入包

      1.core包

        

      2.aop包

        

    三.建立類和方法

    四.建立spring配置文件引入新約束,開啓註解掃描

      

      

    五.註解建立對象

      1.使用註解標記類

    

      2.建立對象有四個註解標記

      

      3.建立對象方式,單例仍是多例

      

    六.註解注入屬性

      1.建立service類,建立dao類

      

      

      2.在service類使用註解注入dao

       方式一.採用自動裝載@Autowired

       

       方式二.採用@Resource獲取特定名稱對象

        

   七.配置文件和註解混合使用

     1.配置文件建立對象注入容器

        

     2.使用註解讓容器實現屬性注入

         

IoC和DI的區別所在

   不少人認爲IoC和DI是一個事物的兩種說法,其實之間存在着細微的不一樣.

    -IoC,控制反轉,將對象交由容器進行管理;

    -DI,依賴注入,將屬性注入於對象之中;

    -DI是依賴於控制反轉技術的,若是使用IoC技術也就沒法使用注入功能.

Spring整合web項目的原理

  1 加載spring核心配置文件,

   

  2 實現思想:把加載配置文件和建立對象過程,在服務器啓動時候完成

  3 實現原理

    (1)ServletContext對象

    (2)監聽器

    (3)具體使用:

  - 在服務器啓動時候,爲每一個項目建立一個ServletContext對象

  - 在ServletContext對象建立時候,使用監聽器能夠具體到ServletContext對象在何時建立

  - 使用監聽器監聽到ServletContext對象建立時候,

    -- 加載spring配置文件,把配置文件配置對象建立

    -- 把建立出來的對象放到ServletContext域對象裏面(setAttribute方法)

  - 獲取對象時候,到ServletContext域獲得 (getAttribute方法)

AOP基礎概念 

  線程中的方法棧

    

 

    java程序虛擬機啓動時會載入程序碼,虛擬機會爲每一條正在運行的線程生成一個方法調用棧,線程以方法運行爲執行單位.

AOP概念以及目標

  AOP是面向切面編程,其實就是在不修改代碼模塊的狀況下在你的模塊中嵌入一些其餘的代碼.

  目標是統一模塊,從而抽取並消除一些散落在系統中塊狀代碼(非業務邏輯).

AOP術語圖解

       

  鏈接點:就是全部線程的方法,能夠做爲嵌入代碼的候選對象;

  切入點:最後被選爲嵌入代碼的對象;

  切面:實現嵌入的對象;

  加強通知:嵌入的內容(一些被定義的方法,包括前置通知,後置通知,異常通知,最終通知,環繞通知)

  織入:嵌入代碼的整個過程被叫作織入

AOP原理

   AOP的出現是有了在業務代碼中嵌入一些非業務代碼,如日誌通知,如鏈接數據庫等事務.

  

  1.修改源代碼,過分耦合無關業務的代碼;

  2.不管是使用繼承方式仍是接口實現方式添加無關業務的代碼,都會使對象間過分耦合;

  3.使用動態代理模式來實現AOP,讓容器去幫咱們進行代碼嵌入.

AOP使用解析(xml)

  1.導入jar包(包括core包和aop包)

    Spring的AOP是基於aspectj框架的,因此在導包時須要導入aspectj的支持包

    

  2.建立spring核心配置文件,導入aop約束

    

  3.配置bean對象,注入至容器中,並配置切面與切點

    

    注:經常使用的表達式

    execution(<訪問修飾符>?<返回類型><方法名>(<參數>)<異常>)

    (1)execution(* com.harry.aop.service.UserService.add(..))

    (2)execution(* com.harry.aop.service.UserServic.*(..))

    (3)execution(* *.*(..))

    (4) 匹配全部save開頭的方法 execution(* save*(..))

AOP使用解析(註解)

  1.開啓aop功能掃描

    

  2.建立bean來做爲切面類和切點類

    

  3.建立切面切點定義

           

事務控制

  事務是什麼?事務控制?

  事務這個詞最先是在數據庫中進行應用,講的用戶定義的一個數據庫操做序列,這些操做要麼全作要麼全不作,是一個不可分割的工做單位。

  事務的管理是指一個事務的開啓,內容添加,提交和回滾.

代碼層次的事務控制

  事務控制本來是在數據庫進行的,但因爲ORM映射後,操做數據庫的語句未必是SQL語句,事務控制也被遷移到了工程語言上(Java/C++/Python).Spring framework支持了事務管理的機制,經過ORM映射後能夠在業務代碼中實現事務控制.

  

事務控制形式

  編程式事務控制

    本身手動控制事務,jdbc和Hibernate提供這種事務管理方式.

    conn.setAutoCommit(false);  //jdbc設置手動控制事務

    session.beginTransaction();  //Hibernate開始一個事務

    這種方式能夠控制到代碼細節,靈活多變.

  聲明式事務控制

    以方法爲單位,能夠在類的方法中織入事務控制的代碼,從而解除了事務代碼與業務代碼的耦合.

    這種方式須要在配置文件中配置,雖然沒法控制到代碼細節(沒法在某個方法中的某幾行加入事務控制),但通常狀況適用.

    

 

Spring事務控制

  利用AOP技術實現聲明式事務控制,包括XML方式和註解方式.

  xml方式    

  1.導入jar包

  2.加入ioc,aop,apectj,tx等約束,並配置數據源,聲明式事務,aop

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    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.xsd
          http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <!-- 1. 數據源對象: C3P0鏈接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>
    
    <!-- 2. JdbcTemplate工具類實例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 3. dao實例 -->
    <bean id="deptDao" class="com.harry.transaction.dao.DeptDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
 
    <!-- 4. service實例 -->
    <bean id="deptService" class="com.harry.transaction.service.DeptService">
        <property name="deptDao" ref="deptDao"></property>
    </bean>
    
    <!-- #############5. Spring聲明式事務管理配置############### -->
    <!-- 5.1 配置事務管理器類 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.2 配置事務加強(若是管理事務?) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 5.3 Aop配置: 攔截哪些方法(切入點表表達式) + 應用上面的事務加強配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* ccm.harry.transaction.service.save*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
    
</beans>

 
applicationContext.xml

  註解方式

  1.導入jar包

  2.xml數據源配置,事務管理類配置,開啓ioc,aop,事務註解掃描.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    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.xsd
          http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    <!-- 1. 數據源對象: C3P0鏈接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>
    
    <!-- 2. JdbcTemplate工具類實例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 事務管理器類 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 開啓註解掃描 -->
    <context:component-scan base-package="com.harry.transaction"></context:component-scan>
    
    <!-- 註解方式實現事務: 指定註解方式實現事務 -->
    <tx:annotation-driven transaction-manager="txManager"/>
    
</beans>      
applicationContext.xml 

  3.在Dao中的類或者類方法上使用@Transactional來代表事務加強的切入點  

    

事務屬性

@Transactional(
            readOnly = false,  // 讀寫事務
            timeout = -1,       // 事務的超時時間不限制
            noRollbackFor = ArithmeticException.class,  // 遇到數學異常不回滾
            isolation = Isolation.DEFAULT,              // 事務的隔離級別,數據庫的默認
            propagation = Propagation.REQUIRED            // 事務的傳播行爲
    )
    public void save(Dept dept){
        deptDao.save(dept);
        int i = 1/0;
        deptDao.save(dept);
    }
txProperty.java

  上述屬性除了事務傳播行爲,其餘都容易理解.重點須要理解事務的傳播行爲.

    Propagation.REQUIRED

      指定當前的方法必須在事務的環境下執行;

      若是當前運行的方法,已經存在事務, 就會加入當前的事務;

    Propagation.REQUIRED_NEW

      指定當前的方法必須在事務的環境下執行;

      若是當前運行的方法,已經存在事務:  事務會掛起; 會始終開啓一個新的事務,執行完後;  剛纔掛起的事務才繼續運行。  

/***************************回滾,日誌不會寫入************************/
Class Log1{
        Propagation.REQUIRED  
        insertLog();  
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 加入當前事務
        .. 異常, 會回滾
        saveDept();
    }
/****************回滾,日誌會開啓新事務並進行寫入****************/
Class Log2{
        Propagation.REQUIRED_NEW  
        insertLog();  
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 始終開啓事務
        .. 異常, 日誌不會回滾
        saveDept();
    }
txPropagation.java
相關文章
相關標籤/搜索