AOP三種簡單配置(1):


1.AopLog.java
    package com.zywang.services.impl2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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 AopLog {
    // 方法執行前調用
    @Before("execution (* com.zywang.services.impl2..*(..))")
    public void before() {
        System.out.println("before");
    }
    // 方法執行後調用
    @After("execution (* com.zywang.services.impl2..*(..))")
    public void after() {
        System.out.println("after");
    }
    // 方法執行的前後調用
    @Around("execution (* com.zywang.services.impl2..*(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("begin around");
        Object object = point.proceed();
        System.out.println("end around");
        return object;
    }
    // 方法運行出現異常時調用
    @AfterThrowing(pointcut = "execution (* com.zywang.services.impl2..*(..))", throwing = "ex")
    public void afterThrowing(Exception ex) {
        System.out.println("afterThrowing");
        System.out.println(ex);
    }
}
===============================================================================
2.ServerT.java
package com.zywang.services.impl2;
import org.springframework.stereotype.Component;
@Component
public class ServerT {
   public void add(){
       System.out.println("你好,我在測試AOP。。。。。");
   }
}

==========================================================================
3.applicationContext3.xml
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章