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

Spring AOP的幾種實(shí)現(xiàn)方式總結(jié)

 更新時間:2017年02月27日 11:41:39   作者:Mercop  
本篇文章主要介紹了Spring AOP的幾種實(shí)現(xiàn)方式總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

AOP核心概念

1、橫切關(guān)注點(diǎn)

對哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點(diǎn)稱之為橫切關(guān)注點(diǎn)

2、切面(aspect)

類是對物體特征的抽象,切面就是對橫切關(guān)注點(diǎn)的抽象

3、連接點(diǎn)(joinpoint)

被攔截到的點(diǎn),因?yàn)閟pring只支持方法類型的連接點(diǎn),所以在Spring中連接點(diǎn)指的就是被攔截到的方法,實(shí)際上連接點(diǎn)還可以是字段或者構(gòu)造器

4、切入點(diǎn)(pointcut)

對連接點(diǎn)進(jìn)行攔截的定義

5、通知(advice)

所謂通知指的就是指攔截到連接點(diǎn)之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類

6、目標(biāo)對象

代理的目標(biāo)對象

7、織入(weave)

將切面應(yīng)用到目標(biāo)對象并導(dǎo)致代理對象創(chuàng)建的過程

8、引入(introduction)

在不修改代碼的前提下,引入可以在運(yùn)行期為類動態(tài)地添加一些方法或字段

Spring 實(shí)現(xiàn)AOP所需要的包:

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

Spring 實(shí)現(xiàn)AOP的方式:

1、Java動態(tài)代理

該方法針對接口的實(shí)例創(chuàng)建代理

applicationContext.xml的配置如下:

<?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" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 
     
    <bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" /> 
  
    <bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" /> 
     
    <aop:config> 
      <aop:aspect id="interceptor" ref="interceptorHandler"> 
        <aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" /> 
        <aop:before method="doSomething" pointcut-ref="addAllMethod" /> 
        <aop:after method="doSomething" pointcut-ref="addAllMethod" /> 
      </aop:aspect> 
    </aop:config> 
</beans> 

其中Abstration為接口,ConcreteImplementor為實(shí)現(xiàn)類,InterceptorHandler為代理攔截類。

public interface <span style="font-size:12px;">Abstration</span> { 
  public void operation() 
} 
//具體實(shí)現(xiàn)化角色 
public class ConcreteImplementor implements Implementor{ 
 
  @Override 
  public void operation() {   
    System.out.println("ConcreteImplementor"); 
  } 
 
} 
public class InterceptorHandler{  
  public void printTime(){ 
    System.out.println("CurrentTime = " + System.currentTimeMillis()); 
  } 
} 

2、CGLIB生成代理

CGLIB針對代理對象為類的情況使用。

通過實(shí)現(xiàn)MethodInterceptor接口,并實(shí)現(xiàn) public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。

3、BeanNameAutoProxyCreator實(shí)現(xiàn)AOP

Spring為我們提供了自動代理機(jī)制,讓容器為我們自動生成代理,把我們從煩瑣的配置工作中解放出來,在內(nèi)部,Spring 使用BeanPostProcessor自動地完成這項(xiàng)工作。

具體配置如下: 

<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>  
<bean  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  <property name="beanNames">  
    <list>  
      <value>*Service</value>  
    </list>  
  </property>  
  <property name="interceptorNames">  
    <list>  
      <value>MyInterceptor</value>  
    </list>  
  </property>  
 </bean> 

其中*Service 為需要攔截代理的bean,以Service結(jié)尾的都 被攔截,并使用MyInterceptor 進(jìn)行攔截,可配置多個攔截器,按順序執(zhí)行。

import java.lang.reflect.Method; 
import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
/** 
 * @author 
 * 
 */  
public class MyInterceptor implements MethodInterceptor{  
  
  @Override  
  public Object invoke(MethodInvocation invocation) throws Throwable {  
      
    Method method = invocation.getMethod();//獲取被攔截的方法  
    Object[] arguments = invocation.getArguments();//獲取攔截方法的參數(shù)  
    /* 
     * 特殊,某些權(quán)限需要做特殊處理 
     * 比如用戶信息權(quán)限,在方法執(zhí)行完畢返回的時候,要將電話號碼與郵箱抹除 
     */  
    //環(huán)繞通知前置特殊處理  
    this.beforeReslove();  
    Object proceed = invocation.proceed();//調(diào)用目標(biāo)方法  
    //環(huán)繞通知后置特殊處理  
    proceed = this.afterReslove();  
    return proceed;  
  } 
   private Object afterReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
     return null; 
   } 
   private void beforeReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
   }   
} 

4、使用注解AspectJ實(shí)現(xiàn)AOP

ApplicationContext.xml 加入

<aop:aspectj-autoproxy/> 

 創(chuàng)建切面處理類

package com.marving.aop; 
import java.util.Arrays; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component;  
@Aspect 
@Component  
public class AspectHandler { 
   
  @Pointcut("execution(* com.marving.service.BaseServ+.*(..))") 
  private void doMethod() { 
  } 
 
    /** 
   * This is the method which I would like to execute before a selected method 
   * execution. 
   */ 
  @Before("doMethod()") 
  public void beforeAdvice() { 
    System.out.println("before method invoked."); 
  } 
 
  /** 
   * This is the method which I would like to execute after a selected method 
   * execution. 
   */ 
  @After("doMethod()") 
  public void afterAdvice() { 
    System.out.println("after method invoked."); 
  } 
 
  // 配置controller環(huán)繞通知,使用在方法aspect()上注冊的切入點(diǎn) 
  @Around("doMethod()") 
  public Object around(ProceedingJoinPoint pjp) throws Throwable{ 
    Object result = null; 
    String methodName = pjp.getSignature().getName(); 
    try { 
      System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs())); 
      result = pjp.proceed(); 
    } catch (Throwable e) { 
      System.out.println("The method [" + methodName + "] occurs expection : " + e); 
      throw new RuntimeException(e); 
    } 
    System.out.println("The method [" + methodName + "] ends"); 
    return result; 
  } 
} 

通過表達(dá)式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入點(diǎn)函數(shù),并使用@Before@After@Around 對所攔截方法執(zhí)行前、中、后進(jìn)行攔截并執(zhí)行處理函數(shù)。

@Around @Before @After三個注解的區(qū)別@Before是在所攔截方法執(zhí)行之前執(zhí)行一段邏輯。@After 是在所攔截方法執(zhí)行之后執(zhí)行一段邏輯。@Around是可以同時在所攔截方法的前后執(zhí)行一段邏輯。

值得注意的是,Around在攔截方法后,需要返回一個方法執(zhí)行結(jié)果,否則,原方法不能正常執(zhí)行。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中的弗洛伊德(Floyd)算法

    Java中的弗洛伊德(Floyd)算法

    這篇文章主要介紹了Java中的弗洛伊德(Floyd)算法,Floyd算法又稱為插點(diǎn)法,是一種利用動態(tài)規(guī)劃的思想尋找給定的加權(quán)圖中多源點(diǎn)之間最短路徑的算法,與Dijkstra算法類似,需要的朋友可以參考下
    2024-01-01
  • PostMan post請求發(fā)送Json數(shù)據(jù)的方法

    PostMan post請求發(fā)送Json數(shù)據(jù)的方法

    下面小編就為大家分享一篇PostMan post請求發(fā)送Json數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • 深入理解JVM自動內(nèi)存管理

    深入理解JVM自動內(nèi)存管理

    對于Java虛擬機(jī)在內(nèi)存分配與回收的學(xué)習(xí),本文主要介紹了JVM自動內(nèi)存管理,文中通過圖文示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • spring Profile如何為不同環(huán)境提供不同的配置支持

    spring Profile如何為不同環(huán)境提供不同的配置支持

    這篇文章主要介紹了spring Profile如何為不同環(huán)境提供不同的配置支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • 微信小程序與Java后端接口交互

    微信小程序與Java后端接口交互

    本文主要介紹了微信小程序與Java后端接口交互,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Spring中@order注解用法實(shí)戰(zhàn)教程

    Spring中@order注解用法實(shí)戰(zhàn)教程

    @Order注解主要用來控制配置類的加載順序,數(shù)字越小,越先加載,下面這篇文章主要給大家介紹了關(guān)于Spring中@order注解用法的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • SpringBoot與SpringMVC第一講

    SpringBoot與SpringMVC第一講

    SpringMVC全名應(yīng)該叫做SpringWebMVC,它其實(shí)是基于servlet來構(gòu)建的一個原始web框架從一開始就包含在了spring框架中,下面通過實(shí)例代碼給大家介紹SpringBoot與SpringMVC的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Java Spring攔截器案例詳解

    Java Spring攔截器案例詳解

    這篇文章主要介紹了Java Spring攔截器案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java對接支付寶支付接口簡單步驟記錄

    java對接支付寶支付接口簡單步驟記錄

    最近項(xiàng)目APP需要接入微信、支付寶支付功能,在分配開發(fā)任務(wù)時,聽說微信支付接口比支付寶支付接口要難實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于java對接支付寶支付接口的簡單步驟,需要的朋友可以參考下
    2024-05-05
  • Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值

    Java求s=a+aa+aaa+aaaa+aa...a 5個數(shù)相加的值

    求s=a+aa+aaa+aaaa+aa...a的值,其中a是一個數(shù)字。例如2+22+222+2222+22222(此時共有5個數(shù)相加),幾個數(shù)相加有鍵盤控制
    2017-02-02

最新評論

仪陇县| 长武县| 大港区| 佛学| 丹江口市| 五原县| 璧山县| 咸宁市| 上林县| 泸溪县| 佛山市| 阳曲县| 米脂县| 临洮县| 塔城市| 法库县| 马关县| 松溪县| 夏津县| 加查县| 确山县| 综艺| 石楼县| 天祝| 陕西省| 盱眙县| 包头市| 延川县| 莲花县| 渭源县| 岐山县| 虹口区| 武定县| 平泉县| 邹平县| 高平市| 宜都市| 皮山县| 安远县| 武功县| 泾川县|