欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringAOP的幾種實現(xiàn)方式總結(jié)

AOP核心概念

阿克陶ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

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

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

2、切面(aspect)

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

3、連接點(joinpoint)

被攔截到的點,因為spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構(gòu)造器

4、切入點(pointcut)

對連接點進行攔截的定義

5、通知(advice)

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

6、目標(biāo)對象

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

7、織入(weave)

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

8、引入(introduction)

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

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

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

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

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

該方法針對接口的實例創(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為實現(xiàn)類,InterceptorHandler為代理攔截類。

public interface <span >Abstration</span> { 
  public void operation() 
} 
//具體實現(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針對代理對象為類的情況使用。

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

3、BeanNameAutoProxyCreator實現(xiàn)AOP

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

具體配置如下: 

<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 進行攔截,可配置多個攔截器,按順序執(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實現(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()上注冊的切入點 
  @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; 
  } 
} 

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章題目:SpringAOP的幾種實現(xiàn)方式總結(jié)
本文路徑:http://aaarwkj.com/article34/peeese.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機電子商務(wù)、網(wǎng)站設(shè)計公司、定制開發(fā)、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
国产精品国产一级国产av| 国产av一区最新精品麻豆| 美女在线观看av少妇| 亚洲精品黄色片中文字幕| 亚洲国产日韩中文字幕| 色综合色综合蘑菇在线| 国产精品一区二区久久毛片| 国产黄色大片一级久久| 女同久久精品国产精品天堂99| 色综合色狠狠天天综合色| 国产精品综合日韩精| 亚洲一区日本一区二区| 草逼免费在线观看视频| 欧美精品熟妇乱黑人最大| 亚洲女人天堂av在线| 少妇人妻偷人精品系列| 国产一区黄片视频在线观看| 全国精品免费视频久久久| 免费福利激情在线播放| 国产做a爰片久久91| 日韩亚洲欧美另类精品| 亚洲女同在线免费观看| 亚洲福利一区二区三区| 日韩在线一区中文字幕| 欧美日韩精品不卡在线播放| 国产亚洲成人精品一区| 日本人妻中文字幕一区| 久久日韩制服丝袜人妻| 日韩精品一区二区国产| 日本东京热免费一二三区| 人妻的秘密一区二区三区| 久久国产精品一区二区三区| 91九色视频免费观看| 91美女人妻精品久久| 日韩暴露一区二区三区| 亚洲国产日韩在线精品| 91九色在线精品一区| 免费搜索国产男女视频| 蜜臀av在线精品国自产拍| 最新天堂av资源在线观看| 欧美激情一区二区亚洲专区|