/**
* 配置aop首先得配置須要aop業務的類 例如 <bean id="helloWorldService" class="com.foreignstudent.util.HelloWorldService" />
* 其次須要配置aop處理類 例如 <bean id="helloWorldAspectBean" class="com.foreignstudent.util.HelloWorldAspect" />
* 而後配置相應的通知方式
* 須要注意的是,需修改返回值的話只能在環繞通知中修改
* 在一個是參數的傳遞,只要看懂配置方式aop,就能夠依葫蘆畫瓢摸索出註解方式
*/
<!-- 須要aop業務的類 -->
<bean id="helloWorldService" class="com.foreignstudent.util.HelloWorldService" />
<!-- aop處理通知類 -->
<bean id="helloWorldAspectBean" class="com.foreignstudent.util.HelloWorldAspect" />
<!-- 配置一個切面 -->
<aop:config>
<aop:aspect id="helloWorldAspect" ref="helloWorldAspectBean">
<aop:pointcut id="helloWorldServicePointcut" expression="execution(* com.foreignstudent.util.*.*(..))" />
<!-- 配置前置通知,這個是自定義的通知類無關緊要,須要配置類和方法,一下雷同 -->
<aop:before pointcut-ref="helloWorldServicePointcut" method="beforeAdvice" />
<!-- 配置前置通知 -->
<aop:after pointcut-ref="helloWorldServicePointcut" method="afterAdvice" />
<!-- 配置後置返回通知 -->
<aop:after-returning pointcut-ref="helloWorldServicePointcut" method="afterReturnAdvice" returning="result" />
<!-- 配置環繞通知 -->
<aop:around pointcut-ref="helloWorldServicePointcut" method="aroundAdvice" />
<!-- 異常通知 -->
<aop:after-throwing pointcut-ref="helloWorldServicePointcut" method="throwingAdvice" throwing="e" />
</aop:aspect>
</aop:config>
<!-- 須要通知的類 -->
package com.foreignstudent.util;
import org.springframework.stereotype.Service;
@Service
public class HelloWorldService {
public String SayHelloWorld(String a) {
System.out.println("Hello World~ " + a);
int a1 = 0/0;
return "執行業務方法的返回值爲:Hello World";
}
}
<!-- 該類已被我修改成註解配置上述內容可去掉註解只保留相應方法便可,須要注意的是參數的傳遞,會在類中說明 -->
package com.foreignstudent.util;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class HelloWorldAspect {
/**
* 該註解爲帶參數,參數名稱需和方法參數變量名稱相同
*/
@Pointcut(value = "execution(* com.foreignstudent.util.*.*(..)) && args(obj)", argNames = "obj")
public void foreignstudent(Object obj) {
}
/** * 該註解爲不帶參數,(返回值 包名。類。方法(參數)) */ @Pointcut(value = "execution(* com.foreignstudent.util.*.*(..))") public void foreign() { } /** * */ @Before(value = "foreignstudent(obj)", argNames = "obj") public void beforeAdvice(Object obj) { System.out.println("前置通知執行了" + obj); } @After(value = "foreignstudent(obj)", argNames = "obj") public void afterAdvice(Object obj) { System.out.println("後置通知執行了"); } @AfterReturning(pointcut = "foreign()", returning = "obj") public void afterReturnAdvice(Object obj) { System.out.println("返回通知執行了" + "運行業務方法返回的結果爲 == " + obj); } @Around(value = "execution(* com.foreignstudent.util.*.*(..))") public String aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { String result = "1"; System.out.println("環繞通知開始執行了"); long start = System.currentTimeMillis(); proceedingJoinPoint.proceed(); long end = System.currentTimeMillis(); System.out.println("環繞通知執行結束了"); System.out.println("執行業務方法共計:" + (end - start) + "毫秒。"); return result; } public void throwingAdvice(JoinPoint joinPoint, Exception e) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("異常通知執行了."); stringBuffer.append("方法:").append(joinPoint.getSignature().getName()).append("出現了異常."); stringBuffer.append("異常信息爲:").append(e.getMessage()); System.out.println(stringBuffer.toString()); }}