最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

舉例講解Java的Spring框架中AOP程序設(shè)計(jì)方式的使用

 更新時(shí)間:2016年04月27日 16:34:00   作者:HotStrong  
這篇文章主要介紹了Java的Spring框架中AOP程序設(shè)計(jì)方式的使用講解,文中舉的AOP下拋出異常的例子非常實(shí)用,需要的朋友可以參考下

1、什么是AOP

AOP是Aspect Oriented Programming的縮寫,意思是面向方面編程,AOP實(shí)際是GoF設(shè)計(jì)模式的延續(xù)。

2、關(guān)于Spring AOP的一些術(shù)語(yǔ):
 A、切面(Aspect):在Spring AOP中,切面可以使用通用類或者在普通類中以@Aspect 注解(@AspectJ風(fēng)格)來實(shí)現(xiàn)
B、連接點(diǎn)(Joinpoint):在Spring AOP中一個(gè)連接點(diǎn)代表一個(gè)方法的執(zhí)行
C、通知(Advice):在切面的某個(gè)特定的連接點(diǎn)(Joinpoint)上執(zhí)行的動(dòng)作。通知有各種類型,其中包括"around"、"before”和"after"等通知。許多AOP框架,包括Spring,都是以攔截器做通知模型, 并維護(hù)一個(gè)以連接點(diǎn)為中心的攔截器鏈
D、切入點(diǎn)(Pointcut):定義出一個(gè)或一組方法,當(dāng)執(zhí)行這些方法時(shí)可產(chǎn)生通知,Spring缺省使用AspectJ切入點(diǎn)語(yǔ)法。

3、通知類型
A、前置通知(@Before):在某連接點(diǎn)(join point)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行(除非它拋出一個(gè)異常)
B、返回后通知(@AfterReturning):在某連接點(diǎn)(join point)正常完成后執(zhí)行的通知:例如,一個(gè)方法沒有拋出任何異常,正常返回
C、拋出異常后通知(@AfterThrowing):方法拋出異常退出時(shí)執(zhí)行的通知
D、后通知(@After):當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)
E、環(huán)繞通知(@Around):包圍一個(gè)連接點(diǎn)(join point)的通知,如方法調(diào)用。這是最強(qiáng)大的一種通知類型,環(huán)繞通知可以在方法調(diào)用前后完成自定義的行為,它也會(huì)選擇是否繼續(xù)執(zhí)行連接點(diǎn)或直接返回它們自己的返回值或拋出異常來結(jié)束執(zhí)行
 
4、@AspectJ風(fēng)格的AOP配置

Spring AOP配置有兩種風(fēng)格:
A、XML風(fēng)格 = 采用聲明形式實(shí)現(xiàn)Spring AOP
B、AspectJ風(fēng)格 = 采用注解形式實(shí)現(xiàn)Spring AOP

5、實(shí)例

切面類TestAspect

package com.spring.aop; 
/** 
 * 切面 
 */ 
public class TestAspect { 
 
  public void doAfter(JoinPoint jp) { 
    System.out.println("log Ending method: " 
        + jp.getTarget().getClass().getName() + "." 
        + jp.getSignature().getName()); 
  } 
 
  public Object doAround(ProceedingJoinPoint pjp) throws Throwable { 
    long time = System.currentTimeMillis(); 
    Object retVal = pjp.proceed(); 
    time = System.currentTimeMillis() - time; 
    System.out.println("process time: " + time + " ms"); 
    return retVal; 
  } 
 
  public void doBefore(JoinPoint jp) { 
    System.out.println("log Begining method: " 
        + jp.getTarget().getClass().getName() + "." 
        + jp.getSignature().getName()); 
  } 
 
  public void doThrowing(JoinPoint jp, Throwable ex) { 
    System.out.println("method " + jp.getTarget().getClass().getName() 
        + "." + jp.getSignature().getName() + " throw exception"); 
    System.out.println(ex.getMessage()); 
  } 
 
  private void sendEx(String ex) { 
    //TODO 發(fā)送短信或郵件提醒 
  } 
}  

package com.spring.service; 
/** 
 * 接口A 
 */ 
public interface AService { 
   
  public void fooA(String _msg); 
 
  public void barA(); 
} 
package com.spring.service; 
/** 
 *接口A的實(shí)現(xiàn)類 
 */ 
public class AServiceImpl implements AService { 
 
  public void barA() { 
    System.out.println("AServiceImpl.barA()"); 
  } 
 
  public void fooA(String _msg) { 
    System.out.println("AServiceImpl.fooA(msg:"+_msg+")"); 
  } 
} 
package com.spring.service; 
 
/** 
 *  Service類B 
 */ 
public class BServiceImpl { 
 
  public void barB(String _msg, int _type) { 
    System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")"); 
    if(_type == 1) 
      throw new IllegalArgumentException("測(cè)試異常"); 
  } 
 
  public void fooB() { 
    System.out.println("BServiceImpl.fooB()"); 
  } 
 
} 

ApplicationContext

<?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:aop="http://www.springframework.org/schema/aop" 
  xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" 
  default-autowire="autodetect"> 
  <aop:config> 
    <aop:aspect id="TestAspect" ref="aspectBean"> 
      <!--配置com.spring.service包下所有類或接口的所有方法--> 
      <aop:pointcut id="businessService" 
        expression="execution(* com.spring.service.*.*(..))" /> 
      <aop:before pointcut-ref="businessService" method="doBefore"/> 
      <aop:after pointcut-ref="businessService" method="doAfter"/> 
      <aop:around pointcut-ref="businessService" method="doAround"/> 
      <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/> 
    </aop:aspect> 
  </aop:config> 
   
  <bean id="aspectBean" class="com.spring.aop.TestAspect" /> 
  <bean id="aService" class="com.spring.service.AServiceImpl"></bean> 
  <bean id="bService" class="com.spring.service.BServiceImpl"></bean> 
 
</beans> 

 測(cè)試類AOPTest

public class AOPTest extends AbstractDependencyInjectionSpringContextTests { 
   
  private AService aService; 
   
  private BServiceImpl bService; 
   
  protected String[] getConfigLocations() { 
    String[] configs = new String[] { "/applicationContext.xml"}; 
    return configs; 
  } 
   
   
  /** 
   * 測(cè)試正常調(diào)用 
   */ 
  public void testCall() 
  { 
    System.out.println("SpringTest JUnit test"); 
    aService.fooA("JUnit test fooA"); 
    aService.barA(); 
    bService.fooB(); 
    bService.barB("JUnit test barB",0); 
  } 
   
  /** 
   * 測(cè)試After-Throwing 
   */ 
  public void testThrow() 
  { 
    try { 
      bService.barB("JUnit call barB",1); 
    } catch (IllegalArgumentException e) { 
       
    } 
  } 
   
  public void setAService(AService service) { 
    aService = service; 
  } 
   
  public void setBService(BServiceImpl service) { 
    bService = service; 
  } 
} 

 
運(yùn)行結(jié)果如下:

log Begining method: com.spring.service.AServiceImpl.fooA 
AServiceImpl.fooA(msg:JUnit test fooA) 
log Ending method: com.spring.service.AServiceImpl.fooA 
process time: 0 ms 
log Begining method: com.spring.service.AServiceImpl.barA 
AServiceImpl.barA() 
log Ending method: com.spring.service.AServiceImpl.barA 
process time: 0 ms 
log Begining method: com.spring.service.BServiceImpl.fooB 
BServiceImpl.fooB() 
log Ending method: com.spring.service.BServiceImpl.fooB 
process time: 0 ms 
log Begining method: com.spring.service.BServiceImpl.barB 
BServiceImpl.barB(msg:JUnit test barB type:0) 
log Ending method: com.spring.service.BServiceImpl.barB 
process time: 0 ms 
 
log Begining method: com.spring.service.BServiceImpl.barB 
BServiceImpl.barB(msg:JUnit call barB type:1) 
log Ending method: com.spring.service.BServiceImpl.barB 
method com.spring.service.BServiceImpl.barB throw exception 
測(cè)試異常 

相關(guān)文章

  • Java線程優(yōu)先級(jí)和守護(hù)線程原理解析

    Java線程優(yōu)先級(jí)和守護(hù)線程原理解析

    這篇文章主要介紹了Java線程優(yōu)先級(jí)和守護(hù)線程原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java匿名類和匿名函數(shù)的概念和寫法

    Java匿名類和匿名函數(shù)的概念和寫法

    匿名函數(shù)寫法和匿名類寫法的前提必須基于函數(shù)式接口匿名函數(shù)寫法和匿名類寫法其本質(zhì)是同一個(gè)東西,只是簡(jiǎn)化寫法不同使用Lambda表達(dá)式簡(jiǎn)寫匿名函數(shù)時(shí),可以同時(shí)省略實(shí)現(xiàn)類名、函數(shù)名,這篇文章主要介紹了Java匿名類和匿名函數(shù)的概念和寫法,需要的朋友可以參考下
    2023-06-06
  • Mapstruct?@Mapper?@Mapping?使用小結(jié)

    Mapstruct?@Mapper?@Mapping?使用小結(jié)

    這篇文章主要介紹了Mapstruct?@Mapper?@Mapping使用小結(jié),他們用于各個(gè)對(duì)象實(shí)體間的相互轉(zhuǎn)換,例如數(shù)據(jù)庫(kù)底層實(shí)體轉(zhuǎn)為頁(yè)面對(duì)象,Model?轉(zhuǎn)為?DTO,?DTO?轉(zhuǎn)為其他中間對(duì)象,?VO?等等,相關(guān)轉(zhuǎn)換代碼為編譯時(shí)自動(dòng)產(chǎn)生的新文件和代碼,需要的朋友可以參考下
    2023-09-09
  • Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式

    Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式

    在命令行中,常見的參數(shù)可以分為三類:選項(xiàng)參數(shù)、非選項(xiàng)參數(shù)和系統(tǒng)參數(shù),本文就來介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下
    2023-09-09
  • Java中數(shù)組和List的互相轉(zhuǎn)換問題小結(jié)

    Java中數(shù)組和List的互相轉(zhuǎn)換問題小結(jié)

    這篇文章主要介紹了Java中數(shù)組和List的互相轉(zhuǎn)換問題小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-03-03
  • 深入了解java NIO之Selector(選擇器)

    深入了解java NIO之Selector(選擇器)

    這篇文章主要介紹了java NIO之Selector(選擇器)的相關(guān)資料,文中講解非常詳細(xì),實(shí)例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • java藍(lán)橋杯試題

    java藍(lán)橋杯試題

    這篇文章主要介紹了java藍(lán)橋杯試題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • MyBatis多數(shù)據(jù)源的兩種配置方式

    MyBatis多數(shù)據(jù)源的兩種配置方式

    這篇文章主要給大家介紹了關(guān)于MyBatis多數(shù)據(jù)源的兩種配置方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Java使用entrySet方法獲取Map集合中的元素

    Java使用entrySet方法獲取Map集合中的元素

    這篇文章主要為大家詳細(xì)介紹了Java使用entrySet方法獲取Map集合中的元素,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • java構(gòu)建OAuth2授權(quán)服務(wù)器

    java構(gòu)建OAuth2授權(quán)服務(wù)器

    本文主要介紹了java構(gòu)建OAuth2授權(quán)服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評(píng)論

章丘市| 龙岩市| 峡江县| 海丰县| 林芝县| 贵阳市| 广水市| 新建县| 乌苏市| 浠水县| 互助| 互助| 丰原市| 扶沟县| 三原县| 太原市| 吉安县| 托里县| 罗平县| 专栏| 黑龙江省| 滦南县| 潍坊市| 洛宁县| 莒南县| 南丰县| 珠海市| 邵东县| 梅州市| 五河县| 勃利县| 凉山| 民丰县| 连山| 荣昌县| 明光市| 延寿县| 顺昌县| 丰顺县| 洛川县| 天水市|