SpringAOP中怎么實(shí)現(xiàn)面向切面編程

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Spring AOP中怎么實(shí)現(xiàn)面向切面編程,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

專(zhuān)注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)吉利免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千多家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

1、概覽

       什么是面向切面編程?

       面向切面編程是一種編程范式(其他常見(jiàn)的編程范式有 面向過(guò)程編程,面向?qū)ο缶幊蘋(píng)OP,面向函數(shù)編程,面向事件驅(qū)動(dòng)編程,面向切面編程),它不是一種編程語(yǔ)言,面向切面編程可以解決特定的問(wèn)題,但是不能解決所有問(wèn)題,它是面向?qū)ο缶幊痰难a(bǔ)充,不是替代。

        它可以很大程度上解決代碼重復(fù)性問(wèn)題,而且可以實(shí)現(xiàn)關(guān)注點(diǎn)分離,比如功能性需求和非功能性需求的分離,從而實(shí)現(xiàn)集中管理,增強(qiáng)代碼的可讀性,可維護(hù)性。

2、AOP常見(jiàn)的使用場(chǎng)景

        在系統(tǒng)開(kāi)發(fā)過(guò)程中常見(jiàn)的使用場(chǎng)景 主要有

        權(quán)限控制

        緩存控制

        事務(wù)控制

        審計(jì)日志

        性能監(jiān)控

        分布式追蹤

        異常處理

3、Spring AOP 兩個(gè)主要關(guān)注點(diǎn)

      Pointcut express

         切面表達(dá)式,主要表達(dá)通過(guò)怎樣的方式找到切面插入的邏輯點(diǎn),pointcut express 提供了豐富的表達(dá)式可以讓我們進(jìn)行切面的插入。

      五種Advice

         找到切入點(diǎn)后,需要明確在什么時(shí)機(jī)進(jìn)行代碼植入,主要有五種,如下:

          @Before 前置通知

          @After(finally) ,后置通知,在方法執(zhí)行完之后切入

          @AfterReturning,返回通知,返回值返回之后

          @AfterThrowing,異常通知,拋出異常之后

           @Around ,環(huán)繞通知,環(huán)繞通知包含了上面所有的類(lèi)型

    以上兩個(gè)關(guān)注點(diǎn) 總結(jié)一句話就是 在什么地方什么時(shí)機(jī)進(jìn)行我們的代碼切入。

4、常見(jiàn)切面表達(dá)式

    1、within表達(dá)式,匹配包或者類(lèi) 下面的方法

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * //匹配ProductService類(lèi)里頭的所有方法
 * @Pointcut("within(com.ruoli.service.ProductService)")
 * //匹配com.ruoli包及子包下所有類(lèi)的方法
 * @Pointcut("within(com.ruoli..*)")
 */
@Aspect
@Component
public class PkgTypeAspectConfig {
   @Pointcut("within(com.ruoli.service.sub.*)")
   public void matchType(){}

   @Before("matchType()")
   public void before(){
       System.out.println("");
       System.out.println("###before");
   }
}

    2、對(duì)象匹配

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * //匹配AOP對(duì)象的目標(biāo)對(duì)象為指定類(lèi)型的方法,即LogService的aop代理對(duì)象的方法
 * @Pointcut("this(com.ruoli.log.Loggable)")
 * //匹配實(shí)現(xiàn)Loggable接口的目標(biāo)對(duì)象(而不是aop代理后的對(duì)象)的方法
 * @Pointcut("target(com.ruoli.log.Loggable)")
 * //this 可以攔截 DeclareParents(Introduction)
 * //target 不攔截 DeclareParents(Introduction)
 * //匹配所有以Service結(jié)尾的bean里頭的方法
 * @Pointcut("bean(*Service)")
 * Created by cat on 2016-12-04.
 */
@Aspect
@Component
public class ObjectAspectConfig {

   @Pointcut("bean(logService)")
   public void matchCondition(){}

   @Before("matchCondition()")
   public void before(){
       System.out.println("");
       System.out.println("###before");
   }
}

   3、參數(shù)匹配,配置指定參數(shù)的方法

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * //匹配任何以find開(kāi)頭而且只有一個(gè)Long參數(shù)的方法
 * @Pointcut("execution(* *..find*(Long))")
 * //匹配任何以find開(kāi)頭的而且第一個(gè)參數(shù)為L(zhǎng)ong型的方法
 * @Pointcut("execution(* *..find*(Long,..))")
 * //匹配任何只有一個(gè)Long參數(shù)的方法
 * @Pointcut("within(com.ruoli..*) && args(Long)")
 * //匹配第一個(gè)參數(shù)為L(zhǎng)ong型的方法
 * @Pointcut("within(com.ruoli..*) && args(Long,..)")
 * Created by cat on 2016-12-04.
 */
@Aspect
@Component
public class ArgsAspectConfig {
   @Pointcut("args(Long,String) && within(com.ruoli.service.*)")
   public void matchArgs(){}

   @Before("matchArgs()")
   public void before(){
       System.out.println("");
       System.out.println("###before");
   }
}

4、注解匹配

    主要有 方法級(jí)別注解,類(lèi)級(jí)別注解,參數(shù)級(jí)別注解。

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * //匹配方法標(biāo)注有AdminOnly的注解的方法
 * @Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
 * //匹配標(biāo)注有NeedSecured的類(lèi)底下的方法 //class級(jí)別
 * @Pointcut("@within(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
 * //匹配標(biāo)注有NeedSecured的類(lèi)及其子類(lèi)的方法 //runtime級(jí)別
 * 在spring context的環(huán)境下,二者沒(méi)有區(qū)別
 * @Pointcut("@target(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
 * //匹配傳入的參數(shù)類(lèi)標(biāo)注有Repository注解的方法
 * @Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
 * Created by cat on 2016-12-04.
 */
@Aspect
@Component
public class AnnoAspectConfig {

   @Pointcut("@args(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
   public void matchAnno(){}

   @Before("matchAnno()")
   public void before(){
       System.out.println("");
       System.out.println("###before");
   }

}

5、execution 表達(dá)式

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * //匹配任何公共方法
 @Pointcut("execution(public * com.ruoli.service.*.*(..))")

 //匹配com.imooc包及子包下Service類(lèi)中無(wú)參方法
 @Pointcut("execution(* com.ruoli..*Service.*())")

 //匹配com.imooc包及子包下Service類(lèi)中的任何只有一個(gè)參數(shù)的方法
 @Pointcut("execution(* com.ruoli..*Service.*(*))")

 //匹配com.imooc包及子包下任何類(lèi)的任何方法
 @Pointcut("execution(* com.ruoli..*.*(..))")

 //匹配com.imooc包及子包下返回值為String的任何方法
 @Pointcut("execution(String com.ruoli..*.*(..))")

 //匹配異常
 execution(public * com.ruoli.service.*.*(..) throws java.lang.IllegalAccessException)

 * 
 */
@Aspect
@Component
public class ExecutionAspectConfig {

	@Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException)")
	public void matchCondition(){}

	@Before("matchCondition()")
	public void before(){
	 System.out.println("");
	 System.out.println("###before");
	}
}

5、五種通知代碼示例

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.Object;
import org.springframework.stereotype.Component;

/**
 * @Before("matchAnno()")
 * @After("matchAnno())") //相當(dāng)于finally
 * @AfterReturning("matchException()")
 * @AfterThrowing("matchException()")
 * @Around("matchException()")
 * @Before(value = "matchLongArg() && args(productId)")
 * public void beforeWithArgs(Long productId)
 * @AfterReturning(value = "matchReturn()",returning = "returnValue")
 * public void getReulst(Object returnValue)
 * 
 */
@Aspect
@Component
public class AdviceAspectConfig {

    /******pointcut********/

    @Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
    public void matchAnno(){}

    @Pointcut("execution(* *..find*(Long)) && within(com.ruoli..*) ")
    public void matchLongArg(){}

    @Pointcut("execution(public * com.ruoli.service..*Service.*(..) throws java.lang.IllegalAccessException) && within(com.ruoli..*)")
    public void matchException(){}

    @Pointcut("execution(String com.ruoli..*.*(..)) && within(com.ruoli..*)")
    public void matchReturn(){}


    /******advice********/
    @Before("matchLongArg() && args(productId)")
    public void before(Long productId){
        System.out.println("###before,get args:"+productId);
    }
   @Around("matchException()")
   public java.lang.Object after(ProceedingJoinPoint joinPoint){
       System.out.println("###before");
       java.lang.Object result = null;
       try{
           result = joinPoint.proceed(joinPoint.getArgs());
           System.out.println("###after returning");
       }catch (Throwable e){
           System.out.println("###ex");
           //throw
           e.printStackTrace();
       }finally {
           System.out.println("###finally");
       }
       return result;
   }

}

上述就是小編為大家分享的Spring AOP中怎么實(shí)現(xiàn)面向切面編程了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:SpringAOP中怎么實(shí)現(xiàn)面向切面編程
當(dāng)前網(wǎng)址:http://muchs.cn/article40/iepeho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站內(nèi)鏈、移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作定制開(kāi)發(fā)、品牌網(wǎng)站制作

廣告

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

成都定制網(wǎng)站建設(shè)