• Spring AOP使用示例


    AOP:【面向切面编程】

    指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。

    1、导入aop模块:Spring AOP:(spring-aspects)
    2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候讲日志进行打印(方法之前、方法运行结束、方法出现异常等)
    3、定义一个日志切面类(LOgAspects);切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行对应的切面方法;
    通知方法:
    前置通知(@Before):logStart:在目标方法div()运行之前运行
    后置通知(@After):logEnd:在目标方法div()运行结束之后运行
    返回通知(@AfterReturning):logReturn:在目标方法div()正常返回之后运行
    异常通知(@AfterThrowing):logException:在目标方法div()出现异常之后运行
    环绕通知:动态代理,手动推进目标方法运行(joinPoint.procced())
    4、给切面类的目标方法标注何时何地运行(通知注解)
    5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
    6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
    7※给配置类中加@EnableAspectJAutoProxy 开启基于注解的AOP模式
    在Spring中很多的@EnableXXX都是表示要开启XXX功能

    主要三步:
    1、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个类是切面类(@Aspect)
    2、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式
    3、开启基于注解的AOP模式;@EnableAspectJAutoProxy

    代码示例:

    
    	4.0.0
    	com.spring
    	spring-annotation
    	0.0.1-SNAPSHOT
    
    	
    		
    			org.springframework
    			spring-context
    			5.1.5.RELEASE
    		
    		
    		
    			org.springframework
    			spring-aspects
    			5.1.5.RELEASE
    		
    
    		
    			junit
    			junit
    			4.10
    			test
    		
    
    		
    		
    		    com.mchange
    		    c3p0
    		    0.9.5.2
    		
    		
    		
    		    mysql
    		    mysql-connector-java
    		    5.1.40
    		
    				
    	
    
    
    package com.spring.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    import com.spring.aop.LogAspects;
    import com.spring.aop.MathCalculator;
    
    
    /**
     * AOP:【动态代理】
     * 		指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式。
     * 
     * 1、导入aop模块:Spring AOP:(spring-aspects)
     * 2、定义一个业务逻辑类(MathCalculator);在业务逻辑运行的时候讲日志进行打印(方法之前、方法运行结束、方法出现异常等)
     * 3、定义一个日志切面类(LOgAspects);切面类里面的方法需要动态感知MathCalculator.div运行到哪里然后执行对应的切面方法;
     * 		通知方法:
     * 				前置通知(@Before):logStart:在目标方法div()运行之前运行
     * 				后置通知(@After):logEnd:在目标方法div()运行结束之后运行
     * 				返回通知(@AfterReturning):logReturn:在目标方法div()正常返回之后运行
     * 				异常通知(@AfterThrowing):logException:在目标方法div()出现异常之后运行
     * 				环绕通知:动态代理,手动推进目标方法运行(joinPoint.procced())
     * 4、给切面类的目标方法标注何时何地运行(通知注解)
     * 5、将切面类和业务逻辑类(目标方法所在类)都加入到容器中;
     * 6、必须告诉Spring哪个类是切面类(给切面类上加一个注解:@Aspect)
     * 7※给配置类中加@EnableAspectJAutoProxy 开启基于注解的AOP模式
     * 		在Spring中很多的@EnableXXX都是表示要开启XXX功能
     *
     * 主要三步:
     * 1、将业务逻辑组件和切面类都加入到容器中;告诉Spring哪个类是切面类(@Aspect)
     * 2、在切面类上的每一个通知方法上标注通知注解,告诉Spring何时何地运行(切入点表达式)
     * 3、开启基于注解的AOP模式;@EnableAspectJAutoProxy
     *
     */
    
    @EnableAspectJAutoProxy
    @Configuration
    public class MainConfigOfAop {
    	
    	//业务逻辑类加入到容器中
    	@Bean
    	public MathCalculator mathCalculator() {
    		System.out.println("mathCalculator bean");
    		return new MathCalculator();
    	}
    
    	//切面类加入到容器中
    	@Bean
    	public LogAspects logAspects() {
    		return new LogAspects();
    	}
    }
    
    
    package com.spring.aop;
    
    public class MathCalculator {
    
    	public int div(int i, int j) {
    		System.out.println("MathCalculator >> div");
    		return i / j;
    	}
    
    }
    
    
    package com.spring.aop;
    
    import java.util.Arrays;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class LogAspects {
    	
    	//抽取公共的切入点表达式
    	//1、本类引用
    	//2、其他的切面引用
    	@Pointcut("execution(public int com.spring.aop.MathCalculator.*(..))")
    	private void pointCut(){};
    	
    	//@Before在目标方法之前切入;切入点表达式(指定在哪个方法切入)
    	//JoinPoint一定要出现在参数列表的第一位
    	@Before(value = "pppointCut()")
    	public void logStart(JoinPoint joinpoint) {
    		System.out.println("logStart>>>>"+joinpoint.getSignature().getName()+">>>>"+Arrays.toString(joinpoint.getArgs()));
    	}
    
    	@After(value ="com.spring.aop.LogAspects.pppointCut()")
    	public void logEnd(JoinPoint joinpoint) {
    		System.out.println("logEnd>>>>>"+joinpoint.getSignature().getName()+">>>>"+Arrays.toString(joinpoint.getArgs()));
    	}
    
    	@AfterReturning(value ="execution(public int com.spring.aop.MathCalculator.*(..))",returning="object")
    	public void logReturn(Object object) {
    		System.out.println("logReturn>>>>"+object);
    	}
    
    	@AfterThrowing(value = "execution(public int com.spring.aop.MathCalculator.*(..))",throwing = "object")
    	public void logException(Exception object) {
    		System.out.println("logException>>>>"+object);
    	}
    
    }
    
    
    package com.spring.test;
    
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import com.spring.aop.MathCalculator;
    import com.spring.config.MainConfigOfAop;
    
    public class IOCTestAOP {
    	@Test
    	public void test01() {
    		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAop.class);
    		
    		MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
    		mathCalculator.div(10, 0);
    		applicationContext.close();
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
  • 相关阅读:
    TensorRT--学习笔记
    Android系统安全 — 5.3-APK V2签名介绍
    场景应用:用SpringBoot手写一个登录拦截器
    Go 复合类型之字典类型介绍
    【四、http】go的http的文件下载
    txt文件恢复如何操作?详细图文教程
    古典密码小记
    Java NIO全面详解(看这篇就够了)
    Git | git的简单使用教程
    ssm+java+vue基于微信小程序的新生自助报到系统#毕业设计
  • 原文地址:https://blog.csdn.net/m0_67403073/article/details/126496588