AOP三种简单配置(1):

来源:互联网 发布:如何自学电脑编程 编辑:程序博客网 时间:2024/06/05 02:00

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
0 0