spring aop使用,spring aop注解,Spring切面编程
================================
©Copyright 蕃薯耀 2020-01-21
https://www.cnblogs.com/fanshuyao/
一、第一步,引用依赖类,在Pom.xml加入依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.1.12.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.1.12.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>5.1.12.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>5.1.12.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>5.1.12.RELEASE</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
二、第二步:增加配置类
1、@Configuration:声明该类为配置类
2、@ComponentScan("com.lqy.spring.aop"):扫描相应的类,纳入spring容器中管理
3、@EnableAspectJAutoProxy:启用注解方式的Aop模式
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.EnableAspectJAutoProxy;
- @Configuration
- @ComponentScan("com.lqy.spring.aop")
- @EnableAspectJAutoProxy
- public class AopConfig {
- }
三、第三步:自定义逻辑运算
- import org.springframework.stereotype.Component;
- /**
- * Calculator类需要在spring容器才能使用aop
- * 使用:@Component,同时使用@ComponentScan注解扫描时,要扫描到该类
- *
- */
- @Component
- public class Calculator {
- public int divInteger(int a, int b) {
- System.out.println("除法运算");
- return a/b;
- }
- public double div(double a, double b) {
- System.out.println("除法运算");
- return a/b;
- }
- public double add(double a, double b) {
- System.out.println("加法运算");
- return a + b;
- }
- }
四、第四步:运算逻辑类切面注入类
1、@Before:方法执行之前
2、@After:方法执行之后(不管会不会出现异常都会执行)
3、@AfterReturning:方法正常执行返回之后
4、@AfterThrowing:方法发生异常执行
- 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;
- import org.springframework.stereotype.Component;
- /**
- * 类需要在spring容器才能使用aop,并且添加切面类的注解:@Aspect
- *
- */
- @Aspect
- @Component
- public class CalculatorAop {
- /**
- * 公共切点
- */
- @Pointcut("execution( * com.lqy.spring.aop.Calculator.*(..))")
- public void pointCut() {}
- /**
- * 方法执行之前
- */
- @Before(value = "execution( * com.lqy.spring.aop.Calculator.*(..))")
- public void before(JoinPoint joinPoint) {
- System.out.println("");
- System.out.println("===============================================================");
- System.out.println("before方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}开始执行:");
- System.out.println("方法参数是:{" + Arrays.asList(joinPoint.getArgs()) + "}");
- }
- /**
- * 方法执行之后(不管会不会出现异常都会执行)
- * pointCut():使用公共的切点表达式
- */
- @After("pointCut()")
- public void after(JoinPoint joinPoint) {
- System.out.println("after方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}执行结束。");
- }
- /**
- * 方法正常执行返回之后
- */
- @AfterReturning(value = "pointCut()", returning = "returnResult")
- public void afterReturn(JoinPoint joinPoint, Object returnResult) {
- System.out.println("afterReturn方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}执行返回的结果是:{" + returnResult + "}。");
- System.out.println("");
- }
- /**
- * 方法出现异常执行
- */
- @AfterThrowing(value = "pointCut()", throwing = "ex")
- public void afterThrowing(JoinPoint joinPoint, Exception ex) {
- System.out.println("afterThrowing方法:{" + joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName() + "}发生异常:" + ex);
- }
- }
五、第五步:测试
- import org.junit.Test;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import com.lqy.spring.aop.Calculator;
- import com.lqy.spring.config.AopConfig;
- public class TestAop {
- private AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class);
- @Test
- public void testDiv() {
- Calculator cal = ac.getBean(Calculator.class);//Calculator类需要在spring容器才能使用aop
- //System.out.println(cal.div(3, 0));
- System.out.println(cal.add(3, 2));
- System.out.println(cal.divInteger(3, 0));
- }
- }
测试结果
- ===============================================================
- before方法:{com.lqy.spring.aop.Calculator.add}开始执行:
- 方法参数是:{[3.0, 2.0]}
- 加法运算
- after方法:{com.lqy.spring.aop.Calculator.add}执行结束。
- afterReturn方法:{com.lqy.spring.aop.Calculator.add}执行返回的结果是:{5.0}。
- 5.0
- ===============================================================
- before方法:{com.lqy.spring.aop.Calculator.divInteger}开始执行:
- 方法参数是:{[3, 0]}
- 除法运算
- after方法:{com.lqy.spring.aop.Calculator.divInteger}执行结束。
- afterThrowing方法:{com.lqy.spring.aop.Calculator.divInteger}发生异常:java.lang.ArithmeticException: / by zero
(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)
================================
©Copyright 蕃薯耀 2020-01-21
https://www.cnblogs.com/fanshuyao/
spring aop使用,spring aop注解,Spring切面编程的更多相关文章
- Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)
三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- Spring学习手札(二)面向切面编程AOP
AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- Spring之控制反转——IoC、面向切面编程——AOP
控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...
- spring入门(四)【面向切面编程】
开发过程中很多时候会用到日志.事务等操作,这些操作如果要写在业务代码中会相当麻烦,这时就会用到面向切面编程(AOP),AOP作为一种编程思想,和OOP有着不同的侧重点,面向对象侧重于万事万物皆对象,而 ...
- Spring详解(五)------面向切面编程
.AOP 什么? AOP(Aspect Oriented Programming),通常称为面向切面编程.它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的 ...
- Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】
接上一篇 Java AOP (1) compile time weaving [Java 切面编程 (1) 编译期织入] Dynamic proxy 动态代理 Befor talking abou ...
- SpringAop@Aspect注解实现切面编程
SpringAOP在springboot中如何使用 #什么是aop## 概念> aop全称Aspect OrientedProgramming,面向切面,AOP主要实现的目的是针对业务处理过程中 ...
- Spring(4)——面向切面编程(AOP模块)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
随机推荐
- Python和Anoconda和Pycharm联合使用教程
简介 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的.大型项目的开发. ...
- 《自拍教程19》aapt_apk信息查看工具
aapt命令行工具介绍 aapt.exe(Linux/Ubuntu/imac操作系统下是未带后缀的aapt), 是android sdk自带的用于打包apk,解析apk的命令行工具软件. aapt.e ...
- 就不能换DB吗? 抽象工厂模式
15.1 就不能换DB吗? 15.2 最基本的数据访问程序 namespace 抽象工厂模式 { class Program { static void Main(string[] args) { U ...
- jsx中的路径拼接
<img style={{height:80,width:80}} src={"/images/"+index+ ".jpg"}/>其中 index ...
- NIO学习笔记,从Linux IO演化模型到Netty—— Linux零拷贝
这里只是感性地认识Linux零拷贝,不涉及具体细节. 1.Linux传统的数据拷贝 用户进程是不能直接访问文件系统的,要先切换到内核态,发起系统调用,DMA把磁盘中的数据写入内核空间,内核再把数据拷贝 ...
- USB-Blaster CPLD FPGA Intel 驱动安装不上的问题,文件的哈希值不在指定的目录文件中,的解决办法,其实很简单
intel的官网的驱动安装文档: https://www.intel.com/content/www/us/en/programmable/support/support-resources/down ...
- Vue与原生APP的相互交互
现在好多APP都采用了Hybrid的开发模式,这种模式特别适合那些内容变动更新较大的APP,从而使得开发和日常维护过程变得集中式.更简短.更经济高效,不需要纯原生频繁发布.但有利肯定有弊咯,性能方面能 ...
- Android数据存储之共享参数SharedPreferences
SharedPreferences是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式,类似于Java的Properties类,二者都是把Key-Value的键值对保存 ...
- ZooKeeper启动报错:My id 3 not in the peer list
错误描述: 解决方法:查看zookeeper-3.4.2/conf目录下 编辑zoo.cfg文件 发现第三行有问题修改
- 【Android休眠】之Android休眠机制
一.休眠概述 休眠,简而言之就是设备在不需要工作的时候把一些部件.外设关掉(掉电或让它进入低功耗模式). 为什么要休眠呢?一言以蔽之:省电. 休眠分主动休眠和被动休眠.主动休眠:比如我电脑不用了,就通 ...