1.在applicationContext中增加aop命名空間
2. pointcut
<!--AOP begin-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="testAspect" class="com.xxxx.fabu.aspect.TestAspect"/>
<!--要求進(jìn)入到AOP的配置,重點(diǎn)配置切入點(diǎn)三痰,以及確定切入的處理操作方法-->
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.xxxx.fabu.service..*.*(..))"/>
<aop:aspect ref="testAspect"><!--進(jìn)行切面的配置槽惫,如果是切面控制應(yīng)該有一個(gè)控制的類-->
<aop:before method="beforeMethod" pointcut-ref="pointCut"/>
<aop:after method="afterMethod" pointcut="execution(* com.xxxx.fabu.service..*.*(..))"/>
</aop:aspect>
</aop:config>
<!--AOP end-->
execution(* com.sample.service.impl..*.*(..))
解釋如下:
符號(hào) | 含義 |
---|---|
execution() | 表達(dá)式的主體 |
第一個(gè)* | 表示任意類型的返回值 |
com.sample.service.impl | AOP所切的服務(wù)的包名职恳,即,我們的業(yè)務(wù)部分 |
包后面的兩個(gè).. | 表示當(dāng)前的包及子包 |
第二個(gè)* | 類名蛹屿,*表示所有類 |
.*(..) | 表示任何方法名屁奏,括號(hào)表示參數(shù),兩個(gè)點(diǎn)表示任何參數(shù)類型 |
3.實(shí)現(xiàn)參數(shù)接收
Java代碼修改:
public class TestAspect {
public void beforeMethod(Object obj) {
if (obj instanceof CheckItem) {
CheckItem item = (CheckItem)obj;
System.out.println("beforeMethod 傳進(jìn)來的參數(shù)"+item.getTitle()+"--"+item.getDetail());
}
System.out.println("beforeMethod*******"+obj);
}
public void afterMethod() {
System.out.println("afterMethod*******");
}
}
修改:applicationContext.xml
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com.xxxx.fabu.service..*.*(..)) and args(id)"/>
<aop:aspect ref="testAspect"><!--進(jìn)行切面的配置错负,如果是切面控制應(yīng)該有一個(gè)控制的類-->
<aop:before method="beforeMethod" pointcut-ref="pointCut" arg-names="id"/>
<aop:after method="afterMethod" pointcut="execution(* com.xxxx.fabu.service..*.*(..))"/>
</aop:aspect>
</aop:config>
4.返回值
5.異常處理
6.環(huán)繞通知
環(huán)繞通知包含了以上所有處理坟瓢,不再需要編寫3個(gè)處理
7.使用Annotation配置AOP
刪除之前的xml配置,添加如下:
<aop:aspectj-autoproxy/>
修改TestAspect.java文件
@Aspect
@Component
public class TestAspect {
@Before(value = "execution(* com.xxxx.fabu.service..*.*(..)) and args(checkItem)", argNames = "checkItem")
public void beforeMethod(Object obj) {
if (obj instanceof CheckItem) {
CheckItem item = (CheckItem)obj;
System.out.println("beforeMethod 傳進(jìn)來的參數(shù)"+item.getTitle()+"--"+item.getDetail());
}
System.out.println("beforeMethod*******"+obj);
}
@After(value = "execution(* com.xxxx.fabu.service..*.*(..))")
public void afterMethod() {
System.out.println("afterMethod*******");
}
@AfterReturning(value = "execution(* com.xxxx.fabu.service..*.*(..))", returning = "v", argNames = "v")
public void returnMethod(Object val) {//處理返回值
System.out.println("returnMethod:"+val);
}
@AfterThrowing(value = "execution(* com.xxxx.fabu.service..*.*(..))", throwing = "e", argNames = "e")
public void throwMethod(Exception e){//對(duì)異常處理
System.out.println("發(fā)生了錯(cuò)誤");
}
@Around(value = "execution(* com.xxxx.fabu.service..*.*(..))")
public Object aroundMethod(ProceedingJoinPoint point) throws Exception {
System.out.println("@@@@@@環(huán)繞通知 aroundMethod ");
return true;
}
}
ps:
1.發(fā)生錯(cuò)誤如下
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'checkServiceImpl' is expected to be of type 'com.xxxx.fabu.service.CheckServiceImpl' but was actually of type 'com.sun.proxy.$Proxy12'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:378)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087)
參考:
http://ekisstherain.iteye.com/blog/1569236
添加:
<aop:aspectj-autoproxy proxy-target-class="true"/>