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

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

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

AOP核心概念

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

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

2、切面(aspect)

類是對(duì)物體特征的抽象,切面就是對(duì)橫切關(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)

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

5、通知(advice)

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

6、目標(biāo)對(duì)象

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

7、織入(weave)

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

8、引入(introduction)

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

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

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

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

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

該方法針對(duì)接口的實(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針對(duì)代理對(duì)象為類的情況使用。

通過(guò)實(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為我們提供了自動(dòng)代理機(jī)制,讓容器為我們自動(dòng)生成代理,把我們從煩瑣的配置工作中解放出來(lái),在內(nèi)部,Spring 使用BeanPostProcessor自動(dòng)地完成這項(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)行攔截,可配置多個(gè)攔截器,按順序執(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í)行完畢返回的時(shí)候,要將電話號(hào)碼與郵箱抹除 
     */  
    //環(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()上注冊(cè)的切入點(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; 
  } 
} 

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

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    SpringBoot與SpringMVC第一講

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

    Java Spring攔截器案例詳解

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

    java對(duì)接支付寶支付接口簡(jiǎn)單步驟記錄

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

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

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

最新評(píng)論

石河子市| 台东县| 游戏| 贺州市| 丰原市| 信丰县| 韩城市| 宜城市| 凤庆县| 高碑店市| 南靖县| 济南市| 襄樊市| 百色市| 九江市| 科技| 婺源县| 赤城县| 南漳县| 田林县| 马关县| 阿瓦提县| 武夷山市| 邵阳县| 乳山市| 阿拉尔市| 甘洛县| 邵阳县| 吴江市| 灵台县| 武乡县| 三门峡市| 东乡族自治县| 留坝县| 玉林市| 临潭县| 连城县| 淮滨县| 黔东| 双城市| 商城县|