Spring入門(六)基於 AOP 的 XML架構

2021-08-22 06:02:33 字數 1682 閱讀 2200

理解aop執行過程

這是xml種的aop配置檔案

expression="execution(* com.tutorialspoint.*.*(..))"/>
該切入點可以執行 com.tutorialspoint報下的所有類的任意含參方法。

aop:before這行是在乙個方法執行之前執行的方法

aop:after這行是在乙個方法執行之後執行的方法

aop:after-returing這行是在乙個方法返回之後不考慮報錯等情況執行的方法,return "retval",retval必須與該方法的引數名相同,因為該方法會攔截他

aop:aftere-throwing這行是在乙個方法報錯之後執行的方法throw "ex" ex 必須與該方法的引數名相同,因為該方法會攔截他

如果想在乙個特定的方法執行的時候執行該建議,可以修改expression;比如設定為student的getname()方法

expression="execution(* com.tutorialspoint.student.getname(..))"/>

注意該方法不能是logging裡面的方法!!!

並且after after-returning方法執行的順序和xml檔案配置的順序有關。

package com.tutorialspoint;

public class logging

/**

* this is the method which i would like to execute

* after a selected method execution.

*/public void afteradvice()

/**

* this is the method which i would like to execute

* when any method returns.

*/public void afterreturningadvice(object retval)

/*** this is the method which i would like to execute

* if there is an exception raised.

*/public void afterthrowingadvice(illegalargumentexception ex)

}

package com.tutorialspoint;

public class student

public integer getage()

public void setname(string name)

public string getname() }

public void printthrowexception()

}

<?xml version="1.0" encoding="utf-8"?>

package com.tutorialspoint;

public static void main(string args)

}

spring的AOP 基於XML實現AOP的過程

參考對應的 logaspect bean 或者 logaspect 類。logaspect類如下 package org.zttc.itat.spring.proxy import org.aspectj.lang.joinpoint import org.aspectj.lang.proceedi...

Spring 基於註解的AOP

用於記錄日誌的工具類,它裡面提供了公共的 component logger aspect 表示當前類是乙個切面類 public class logger 前置通知 before pt1 public void beforeprintlog 後置通知 afterreturning pt1 public...

Spring中基於註解的AOP

spring提供了基於註解的aop。開啟配置 在配置檔案中配置 前置通知 切點類package cn.belle.test public class helloworldservice 切面類package cn.belle.test import org.aspectj.lang.annotati...