Spring AOP(3)基於XML配置實現的示例

2021-07-16 16:46:00 字數 1352 閱讀 1654

使用註解的方式配置切面類在程式出問題的時候不方便查詢問題,尤其是aop這種tricky的功能,因此在spring配置檔案中配置aop是乙個相對好的選擇(個人感覺):

<

aop:config

>

<

aop:pointcutid=

"pointcut"

expression

="execution (* crm.service.*.*(..))"

/>

<

aop:aspect

ref=

"aspect"

>

<

aop:before

method

="before"

pointcut-ref

="pointcut"

/>

<

aop:after-returning

method

="afterreturning"

pointcut-ref

="pointcut"

returning

="retval"

/>

<

aop:around

method

="around"

pointcut-ref

="pointcut"

/>

<

aop:after

method

="afteradvice"

pointcut-ref

="pointcut"

/>

<

aop:after-throwing

method

="afterthrowing"

arg-names

="ex"

throwing

="ex"

pointcut-ref

="pointcut"

/>

aop:aspect

>

aop:config

>

<

beanid=

"aspect"

class

="crm.aop.xmllogaspect"

/>

xmllogaspect功能和之前的logaspect類一致,只是減去註解。

是宣告切入點。

宣告切面

宣告前置通知、後置通知

後置返回通知需要配置returning,且規則與註解方式一致,需要保持與後置返回通知方法的引數名一致。

後置異常通知,需要配置args-names和throwing,且保持與方法中異常的引數名一致

Spring AOP 基於XML檔案的配置

spring aop的配置可以基於註解,也可以基於xml檔案。前面幾篇都是使用註解的方式。下面介紹下使用xml檔案如何配置 使用的測試類和切面類都類似。只需要屬於aop的註解去掉即可。下面是aop的xml配置 1 xml version 1.0 encoding utf 8 2 beans xmln...

七 SpringAOP入門案例(基於xml配置)

spring的aop是面向切面程式設計的意思,不需要改變原有 的基礎上對原有 進行增強 我們來看入門案例 首先建立乙個service介面 package com.lp.service date 2020 5 29 14 37 author luopeng public inte ce account...

基於註解的Spring AOP

spring aop 基本概念 url joinpoint api url 1.定義註解 target retention retentionpolicy.runtime documented public inte ce testannotation 2.定義切面 切面 aspect 在sprin...