Spring阶段性学习总结(十 一)Aspect实现各种通知

1 package SpringAopImp;
2 
3 public interface Calculator {
4     int add(int i, int j);
5     int sub(int i, int j);
6     int mul(int i, int j);
7     int div(int i, int j);
8 }

 

 1 package SpringAopImp;
 2 
 3 
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class CalculatorImp  implements Calculator {
 8        
 9     @Override
10     public int add(int i, int j) {
11         int result=i+j;
12         System.out.println("Method is Running");
13         return result;
14     }
15 
16     @Override
17     public int sub(int i, int j) {
18         int result=i-j;
19         return result;
20     }
21 
22     @Override
23     public int mul(int i, int j) {
24         int result=i*j;
25         return result;
26     }
27 
28     @Override
29     public int div(int i, int j) {
30         int result=i/j;
31         return result;
32     }
33 }

 

 1 package SpringAopImp;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.annotation.*;
 5 import org.springframework.stereotype.Component;
 6 
 7 import java.util.Arrays;
 8 import java.util.List;
 9 
10 //把这个类声明为一个切面:1、把该类放入到IOC容器中
11 
12 @Component
13 @Aspect
14 public class LoggingAspect {
15 
16     //前置通知在目标方法开始前执行
17     @Before("execution(public int SpringAopImp.CalculatorImp.add(int ,int))")
18     public void beforeMethod(JoinPoint joinPoint) {
19         String methodName = joinPoint.getSignature().getName();
20         List args = Arrays.asList(joinPoint.getArgs());
21         System.out.println("The Method:" + methodName + " beginWith:" + args);
22     }
23 
24     //后置通知,在目标方法开始后执行,无论方法是否发生异常
25     @After("execution(public int SpringAopImp.CalculatorImp.add(int ,int))")
26     public void afterMethod(JoinPoint joinPoint) {
27         String methodName = joinPoint.getSignature().getName();
28         System.out.println("The Method:" + methodName + " ends");
29     }
30 
31     //返回通知,在目标方法正常结束后执行的代码,返回通知是可以返回方法的返回值的
32     @AfterReturning(value = "execution(public int SpringAopImp.CalculatorImp.add(int ,int))", returning = "result")
33     public void afterReturning(JoinPoint joinPoint, Object result) {
34         String methodName = joinPoint.getSignature().getName();
35         System.out.println("The Method:" + methodName + " endsWith:" + result);
36     }
37 
38     /*
39      * 异常通知,在目标方法出现异常的时候,调用代码
40      * 可以访问异常对象;且可以指定在出现特定异常时在执行通知代码
41      * */
42     @AfterThrowing(value = "execution(public int SpringAopImp.CalculatorImp.add(int ,int))", throwing = "ex")
43     public void afterThrowing(JoinPoint joinPoint, Exception ex) {
44         String methodName = joinPoint.getSignature().getName();
45         System.out.println("The Method :" + methodName + "has Exception: " + ex);
46     }
47 
48 } 
    
   

 

 1 xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        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">
 7 
 8         <context:component-scan base-package="SpringAopImp">context:component-scan>
 9         
10       <aop:aspectj-autoproxy>aop:aspectj-autoproxy>
11 beans>

 

 1 package SpringAopImp;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("SpringAopImp/SpringAopImp.xml");
 8         Calculator calculatorImp = (Calculator) ctx.getBean("calculatorImp");
 9         System.out.println(calculatorImp.getClass().getName());
10         System.out.println(calculatorImp.add(1,3));
11     }
12 }

 

转载于:https://www.cnblogs.com/zhang188660586/p/11583590.html

你可能感兴趣的:(java)