首先在因為要使用到掃瞄功能,所以xml的標頭檔案中除了引入bean和aop之外,還要引入context才行:
<?xml version="1.0" encoding="utf-8"?>
...
x11
<?xmlversion="1.0" encoding="utf-8"?>
<beans
xmlns=""
xmlns:xsi=""
xmlns:context=""
xmlns:aop=""
xsi:schemalocation="
/spring-beans.xsd
/spring-aop.xsd
/spring-context.xsd">
...
beans
>
既然使用註解,那麼在配置檔案中需要開啟掃瞄配置以註冊bean元件;同時spring中使用了aspectj包的@aspect註解標註當前元件為切面,所以同時還需要在配置檔案中配置實用aspectj的自動**模式。如下:
1
<context:component-scan
base-package="dulk.learn"
>
context:component-scan
>
<aop:aspectj-autoproxy
>
aop:aspectj-autoproxy
>
aop的註解配置方式,對於乙個類來說:
import org.aspectj.lang.joinpoint;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.after;
import org.aspectj.lang.annotation.afterreturning;
import org.aspectj.lang.annotation.afterthrowing;
import org.aspectj.lang.annotation.around;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.before;
import org.springframework.stereotype.component;
@component
@aspect
public class section
@afterreturning(pointcut = "execution(* dulk.learn..*.*(..))", returning = "ret")
public void doafterreturning(joinpoint point, object ret)
@afterthrowing(pointcut = "execution(* dulk.learn..*.*(..))", throwing = "e")
public void dothrow(joinpoint point, throwable e)
@after("execution(* dulk.learn..*.*(..))")
public void doafter()
@around("execution(* dulk.learn..*.*(..))")
public void doaround(proceedingjoinpoint point) catch (throwable throwable)
system.out.println("doaround-doafter");
}}
x1
49
importorg.aspectj.lang.joinpoint;
importorg.aspectj.lang.proceedingjoinpoint;
importorg.aspectj.lang.annotation.after;
importorg.aspectj.lang.annotation.afterreturning;
importorg.aspectj.lang.annotation.afterthrowing;
importorg.aspectj.lang.annotation.around;
importorg.aspectj.lang.annotation.aspect;
importorg.aspectj.lang.annotation.before;
importorg.springframework.stereotype.component;
@component
@aspect
publicclass
section
@afterreturning(pointcut="execution(* dulk.learn..*.*(..))", returning
="ret")
publicvoid
doafterreturning(joinpoint
point, object
ret)
@afterthrowing(pointcut="execution(* dulk.learn..*.*(..))", throwing
="e")
publicvoid
dothrow(joinpoint
point, throwable
e)
@after("execution(* dulk.learn..*.*(..))")
publicvoid
doafter()
@around("execution(* dulk.learn..*.*(..))")
publicvoid
doaround(proceedingjoinpoint
point) catch (throwable
throwable)
system.out.println("doaround-doafter");
}
}
基於註解的AOP配置
before 前置通知 afterreturning 後置通知 after 最終通知 afterthrowing 異常通知 around 環繞通知 pointcut 指定切入點表示式 使用方法 pointcut execution cn.itcast.service.impl.private voi...
基於註解配置的AOP
首先在spring的xml檔案中完成相應的配置 在持久化類中加上註解,方便使用id呼叫 注意這裡設定了乙個異常 1 0的異常 日誌類的 如下 import org.aspectj.lang.annotation.import org.springframework.stereotype.compon...
Spring之Aop的註解配置
1.首先要新增jar包 2.寫方法介面 public inte ce calculate 3.寫方法的實現類,並把其加入到ioc容器中去 component public class calculateimpl implements calculate public int sub int i,in...