注解实现的spring-aop

接口:

 1 package spring.aop;
 2 
 3 public interface Arithmetic {
 4 
 5     Integer add(Integer a,Integer b);
 6     Integer sub(Integer a,Integer b);
 7     Integer div(Integer a,Integer b);
 8 
 9 
10 }

实现类:

 1 package spring.aop;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component
 6 public class ArithmeticImpl implements Arithmetic{
 7 
 8     @Override
 9     public Integer add(Integer a, Integer b) {
10         System.out.println("add -> "+(a+b));
11         return a+b;
12     }
13 
14     @Override
15     public Integer sub(Integer a, Integer b) {
16         System.out.println("sub -> "+(a-b));
17         return a-b;
18     }
19 
20     @Override
21     public Integer div(Integer a, Integer b) {
22         System.out.println("div -> "+(a/b));
23         return a/b;
24     }
25 
26 
27 }

配置文件:aop.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--


     -->

    <context:component-scan base-package="spring.aop"/>

    <!--自动为匹配的类生成代理对象-->
    <aop:aspectj-autoproxy proxy-target-class="true" />

</beans>

验证切面:

 1 package spring.aop;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.core.annotation.Order;
 7 import org.springframework.stereotype.Component;
 8 
 9 import java.util.Arrays;
10 
11 @Order(1)
12 @Aspect
13 @Component
14 public class VliAspect {
15 
16     @Before("spring.aop.aspect.declareJointPointExpression()")
17     public void validateArgs(JoinPoint joinPoint){
18 
19         System.out.println("validateArgs -> "+ Arrays.asList(joinPoint.getArgs()));
20     }
21 }

主切面

 1 package spring.aop;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.*;
 5 import org.springframework.core.annotation.Order;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.util.Arrays;
 9 import java.util.List;
10 
11 //声明这个切面的优先级,值越小,优先级越高
12 @Order(2)
13 //声明这个类是一个切面
14 @Aspect
15 @Component
16 public class aspect {
17 
18     //定义一个方法,用于声明切点表达式,一般该方法不写其他代码
19     @Pointcut("execution( * spring.aop.*.*(*,*))")
20     public void declareJointPointExpression(){}
21 
22     //声明该方法是一个前置通知
23     @Before("declareJointPointExpression()")
24     public void beforeMethod(JoinPoint joinPoint){
25         String methodName = joinPoint.getSignature().getName();
26         List<Object> args = Arrays.asList(joinPoint.getArgs());
27         System.out.println("methodName:"+methodName+"--->before args: "+args);
28     }
29 
30 
31     //声明该方法是一个后置通知,无论是否发生异常,都会在目标方法执行后执行
32     //后置通知无法访问目标方法的执行结果
33     @After("declareJointPointExpression()")
34     public void afterMethod(JoinPoint joinPoint){
35         String methodName = joinPoint.getTarget().getClass().getName();
36         List<Object> args = Arrays.asList(joinPoint.getArgs());
37         System.out.println("methodName:"+methodName+"--->after args: "+args);
38     }
39 
40     //返回通知,目标方法正常结束后执行
41     //可以访问方法的返回值
42     @AfterReturning(value = "declareJointPointExpression()",returning = "result")
43     public void afterReturning(JoinPoint joinPoint,Object result){
44         String methodName = joinPoint.getSignature().getName();
45         System.out.println("methodName:"+methodName+"--->afterReturning args: "+result);
46 
47     }
48 
49     @AfterThrowing(value = "declareJointPointExpression())",throwing = "e")
50     public void afterThrowing(JoinPoint joinPoint,Exception e){
51         String methodName = joinPoint.getSignature().getName();
52         System.out.println("methodName:"+methodName+"--->afterThrowing args: "+e);
53 
54     }
55 }

测试类

package spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args){

        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:aop.xml");
        Arithmetic proxy = ac.getBean("arithmeticImpl",ArithmeticImpl.class);
        proxy.add(3,5);
        System.out.println("-----------------------");
        proxy.sub(3,0);
        System.out.println("-----------------------");
        proxy.div(3,0);
        System.out.println("-----------------------");
    }
}

结果

猜你喜欢

转载自www.cnblogs.com/kill-9/p/9647347.html