AspectJ完整教程

AspectJ

AspectJ教程详情访问

https://www.yiibai.com/spring_aop/

bean配置

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

              http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

      

   

   

 

切面类型

@Aspect

@Component

public class LoggingAsceptJ {

 

       @Pointcut("execution(*com.imnu.dproxy.ArithmeticCalculatorImpl.*(..))")

       privatevoid selectAll() {

       }

 

       @Before("selectAll()")

       publicvoid beforeMethod(JoinPoint jpJoinPoint) {

              Stringname = jpJoinPoint.getSignature().getName();

              Listarg = Arrays.asList(jpJoinPoint.getArgs());

              System.out.println("这是前置通知Name" + name + "arg:" + arg);

       }

 

       @After("selectAll()")

       publicvoid afterMethod(JoinPoint jpJoinPoint) {

              Stringname = jpJoinPoint.getSignature().getName();

              Listarg = Arrays.asList(jpJoinPoint.getArgs());

              System.out.println("这是后置通知Name" + name + "arg:" + arg);

       }

 

       @AfterReturning(pointcut= "selectAll()", returning = "retVal")

       publicvoid afterReturning(JoinPoint jpJoinPoint, Object retVal) {

              Stringname = jpJoinPoint.getSignature().getName();

              Listarg = Arrays.asList(jpJoinPoint.getArgs());

              System.out.println("这是返回通知" + name + "arg:" + arg + "retVal:" +retVal);

       }

 

       @AfterThrowing(pointcut= "selectAll()", throwing = "error")

       publicvoid afterThrowingAdvice(JoinPoint jp, Throwable error) {

              System.out.println("[afterThrowingAdvice]Method Signature: " + jp.getSignature());

              System.out.println("[afterThrowingAdvice]Exception: " + error);

       }

       /**

        * 环绕通知需要携带ProceedingJoinPoint类型的参数

        * 环绕通知类似于动态代理的全过程proceedingJoinPoint类型参数可以决定是否执行目标方法

        * 且环绕通知必须有返回值,返回值为目标方法的返回值

        * @param proceedingJoinPoint

        */

       @Around(value= "selectAll()")

       publicObject anroudMethod(ProceedingJoinPoint proceedingJoinPoint) {

              Objectresult = null;

              StringmethodName = proceedingJoinPoint.getSignature().getName();

              try{

                     System.out.println("前置通知");

                     result= proceedingJoinPoint.proceed();

                     System.out.println("后置通知");

              }catch (Throwable e) {

                    System.out.println("异常通知");

                     e.printStackTrace();

              }

              returnresult;

       }

      

}

 

Pom.xml文件

    <dependencies>

        <dependency>

            <groupId>org.springframeworkgroupId>

            <artifactId>spring-aopartifactId>

            <version>4.1.0.RELEASEversion>

        dependency>

        <dependency>

            <groupId>org.springframeworkgroupId>

            <artifactId>spring-contextartifactId>

            <version>4.1.4.RELEASEversion>

        dependency>

        <dependency>

            <groupId>org.aspectjgroupId>

            <artifactId>aspectjweaverartifactId>

            <version>1.6.8version>

        dependency>

        <dependency>

            <groupId>junitgroupId>

            <artifactId>junitartifactId>

            <version>3.8.1version>

            <scope>testscope>

        dependency>

    dependencies>

 

测试代码

ApplicationContext  context = new ClassPathXmlApplicationContext("com/imnu/dproxy/bean.xml");

ArithmeticCalculator ari =(ArithmeticCalculator) context.getBean("arithmeticcalculator");

int resout = ari.add(1, 2);

System.out.println(resout);

 


你可能感兴趣的:(spring)