成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SpringAOP中怎么實現面向切面編程

這期內容當中小編將會給大家?guī)碛嘘PSpring AOP中怎么實現面向切面編程,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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

1、概覽

       什么是面向切面編程?

       面向切面編程是一種編程范式(其他常見的編程范式有 面向過程編程,面向對象編程OOP,面向函數編程,面向事件驅動編程,面向切面編程),它不是一種編程語言,面向切面編程可以解決特定的問題,但是不能解決所有問題,它是面向對象編程的補充,不是替代。

        它可以很大程度上解決代碼重復性問題,而且可以實現關注點分離,比如功能性需求和非功能性需求的分離,從而實現集中管理,增強代碼的可讀性,可維護性。

2、AOP常見的使用場景

        在系統(tǒng)開發(fā)過程中常見的使用場景 主要有

        權限控制

        緩存控制

        事務控制

        審計日志

        性能監(jiān)控

        分布式追蹤

        異常處理

3、Spring AOP 兩個主要關注點

      Pointcut express

         切面表達式,主要表達通過怎樣的方式找到切面插入的邏輯點,pointcut express 提供了豐富的表達式可以讓我們進行切面的插入。

      五種Advice

         找到切入點后,需要明確在什么時機進行代碼植入,主要有五種,如下:

          @Before 前置通知

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

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

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

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

    以上兩個關注點 總結一句話就是 在什么地方什么時機進行我們的代碼切入。

4、常見切面表達式

    1、within表達式,匹配包或者類 下面的方法

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

/**
 * //匹配ProductService類里頭的所有方法
 * @Pointcut("within(com.ruoli.service.ProductService)")
 * //匹配com.ruoli包及子包下所有類的方法
 * @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、對象匹配

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

/**
 * //匹配AOP對象的目標對象為指定類型的方法,即LogService的aop代理對象的方法
 * @Pointcut("this(com.ruoli.log.Loggable)")
 * //匹配實現Loggable接口的目標對象(而不是aop代理后的對象)的方法
 * @Pointcut("target(com.ruoli.log.Loggable)")
 * //this 可以攔截 DeclareParents(Introduction)
 * //target 不攔截 DeclareParents(Introduction)
 * //匹配所有以Service結尾的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、參數匹配,配置指定參數的方法

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

/**
 * //匹配任何以find開頭而且只有一個Long參數的方法
 * @Pointcut("execution(* *..find*(Long))")
 * //匹配任何以find開頭的而且第一個參數為Long型的方法
 * @Pointcut("execution(* *..find*(Long,..))")
 * //匹配任何只有一個Long參數的方法
 * @Pointcut("within(com.ruoli..*) && args(Long)")
 * //匹配第一個參數為Long型的方法
 * @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、注解匹配

    主要有 方法級別注解,類級別注解,參數級別注解。

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;

/**
 * //匹配方法標注有AdminOnly的注解的方法
 * @Pointcut("@annotation(com.ruoli.anno.AdminOnly) && within(com.ruoli..*)")
 * //匹配標注有NeedSecured的類底下的方法 //class級別
 * @Pointcut("@within(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
 * //匹配標注有NeedSecured的類及其子類的方法 //runtime級別
 * 在spring context的環(huán)境下,二者沒有區(qū)別
 * @Pointcut("@target(com.ruoli.anno.NeedSecured) && within(com.ruoli..*)")
 * //匹配傳入的參數類標注有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 表達式

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類中無參方法
 @Pointcut("execution(* com.ruoli..*Service.*())")

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

 //匹配com.imooc包及子包下任何類的任何方法
 @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())") //相當于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中怎么實現面向切面編程了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道。

新聞標題:SpringAOP中怎么實現面向切面編程
當前網址:http://jinyejixie.com/article40/iepeho.html

成都網站建設公司_創(chuàng)新互聯,為您提供外貿建站網站內鏈、移動網站建設、企業(yè)網站制作、定制開發(fā)、品牌網站制作

廣告

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

成都定制網站建設
富宁县| 客服| 石渠县| 建平县| 南宁市| 南陵县| 遂川县| 遂川县| 广州市| 聊城市| 凉城县| 博乐市| 淮南市| 霞浦县| 唐海县| 西畴县| 常山县| 扶风县| 凌源市| 祁门县| 罗江县| 庆元县| 类乌齐县| 青川县| 峨边| 平邑县| 灵台县| 贵溪市| 修武县| 内乡县| 涪陵区| 札达县| 台湾省| 特克斯县| 遂平县| 鄢陵县| 泌阳县| 饶平县| 竹溪县| 房产| 石嘴山市|