【 Aspect 案例讲解(基于注解) 】

第1步:声明使用注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置扫描注解的位置 -->
    <context:component-scan base-package="cn.ys"/>

    <!-- 配置aop注解生效-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

第2步:替换 service 和 切面 bean

要替换的:

    <!--  配置UserService-->
    <bean id="userService" class="cn.ys.service.UserServiceImpl"></bean>

    <!-- 配置切面对象-->
    <bean id="myAspect" class="cn.ys.aspect.MyAspect"></bean>

【 Aspect 案例讲解(基于注解) 】
【 Aspect 案例讲解(基于注解) 】

第3步:声明切面

【 Aspect 案例讲解(基于注解) 】

【 Aspect 案例讲解(基于注解) 】

第4步:声明公共切入点

package cn.ys.aspect;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

}

第5步:声明前置通知

package cn.ys.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void myBefore(JoinPoint jp){
        System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名
    }

}

第6步:声明后置通知

package cn.ys.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void myBefore(JoinPoint jp){
        System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名
    }

    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param jp
     */
    // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint jp,Object retValue){
        System.out.println("3.后置通知..." +  jp.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }

}

第7步:声明环绕通知

package cn.ys.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void myBefore(JoinPoint jp){
        System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名
    }

    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param jp
     */
    // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint jp,Object retValue){
        System.out.println("3.后置通知..." +  jp.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }

    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("2.环绕通知...开启事务..." + pjp.getSignature().getName());
        //放行
        Object retObj = pjp.proceed();
        System.out.println("4.环绕通知....提交事务...");
        return retObj;
    }

}

第8步:声明异常通知

package cn.ys.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void myBefore(JoinPoint jp){
        System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名
    }

    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param jp
     */
    // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint jp,Object retValue){
        System.out.println("3.后置通知..." +  jp.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }

    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("2.环绕通知...开启事务..." + pjp.getSignature().getName());
        //放行
        Object retObj = pjp.proceed();
        System.out.println("4.环绕通知....提交事务...");
        return retObj;
    }

    @AfterThrowing(pointcut = "myPointcut()",throwing = "e")
    public void myAfterThrowing(JoinPoint jp,Throwable e){
        System.out.println("异常通知..." + jp.getSignature().getName() + "===" + e.getMessage() );
    }

}

第9步:声明最终通知

package cn.ys.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    //声明一个公共的切入点
    @Pointcut("execution(* cn.ys.service.UserServiceImpl.*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void myBefore(JoinPoint jp){
        System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名
    }

    /**
     * 后置通知获取service方法执行后的返回值
     * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning
     * @param jp
     */
    // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/>
    @AfterReturning(pointcut = "myPointcut()",returning = "retValue")
    public void myAfterReturning(JoinPoint jp,Object retValue){
        System.out.println("3.后置通知..." +  jp.getSignature().getName());
        System.out.println("返回值:" + retValue);
    }

    @Around("myPointcut()")
    public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("2.环绕通知...开启事务..." + pjp.getSignature().getName());
        //放行
        Object retObj = pjp.proceed();
        System.out.println("4.环绕通知....提交事务...");
        return retObj;
    }

    @AfterThrowing(pointcut = "myPointcut()",throwing = "e")
    public void myAfterThrowing(JoinPoint jp,Throwable e){
        System.out.println("异常通知..." + jp.getSignature().getName() + "===" + e.getMessage() );
    }

    @After("myPointcut()")
    public void myAfter(JoinPoint jp){
        System.out.println("最终通知..." + jp.getSignature().getName());
    }
}

测试

package cn.ys.spring;

import cn.ys.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpring {

    @Test
    public void testSpringCglib() throws Exception {

        //获取Spring容器中代理对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = (UserService) context.getBean("userService");

        userService.deleteUser();

    }

}

控制台打印

C:\Java\jdk1.8.0_181\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\lib\idea_rt.jar=51688:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\plugins\junit\lib\junit-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.1\plugins\junit\lib\junit5-rt.jar;C:\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Java\jdk1.8.0_181\jre\lib\rt.jar;C:\Users\ys951\Desktop\JavaSETest\JavaEE\Spring\target\test-classes;C:\Users\ys951\Desktop\JavaSETest\JavaEE\Spring\target\classes;C:\Users\ys951\.m2\repository\org\springframework\spring-core\4.3.19.RELEASE\spring-core-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\org\springframework\spring-beans\4.3.19.RELEASE\spring-beans-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\org\springframework\spring-context\4.3.19.RELEASE\spring-context-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\org\springframework\spring-expression\4.3.19.RELEASE\spring-expression-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\ys951\.m2\repository\org\springframework\spring-aop\4.3.19.RELEASE\spring-aop-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\ys951\.m2\repository\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;C:\Users\ys951\.m2\repository\org\springframework\spring-aspects\4.3.19.RELEASE\spring-aspects-4.3.19.RELEASE.jar;C:\Users\ys951\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\ys951\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 cn.ys.spring.testSpring,testSpringCglib
二月 27, 2019 10:52:32 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@555590: startup date [Wed Feb 27 10:52:32 CST 2019]; root of context hierarchy
二月 27, 2019 10:52:32 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2.环绕通知...开启事务...deleteUser
1.前置通知...deleteUser
删除用户......
4.环绕通知....提交事务...
最终通知...deleteUser
3.后置通知...deleteUser
返回值:null

Process finished with exit code 0