Spring 基于注解的AOP面向切面编程
Spring 基于注解的AOP面向切面编程
代码实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjw</groupId>
<artifactId>day03_eesy_05annotationAOP</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<encoding>UTF-8</encoding>
<spring.version>6.1.1</spring.version>
<aspectjweaver.version>1.9.21</aspectjweaver.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
</dependencies>
</project>
Spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.zjw"/>
<!--配置spring开启注解AOP的支持-->
<aop:aspectj-autoproxy/>
</beans>
Service层
package com.zjw.service.impl;
import com.zjw.service.IAccountService;
import org.springframework.stereotype.Service;
/**
* @author zjw
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Override
public void saveAccount() {
System.out.println("saveAccount().....");
int i = 1/0;
}
@Override
public void updateAccount(int i) {
System.out.println("updateAccount(int i)...."+i);
}
@Override
public int deleteAccount() {
System.out.println("deleteAccount()......");
return 0;
}
}
切面类
package com.zjw.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* 用于记录日志的工具类,它里面提供了公共的代码
* Aspect注解 表示当前类是一个切面类
* @author zjw
*/
@Component("logger")
@Aspect
public class Logger {
@Pointcut("execution(* com.zjw.service.impl.*.*(..))")
private void pt1(){}
/**
* 前置通知
*/
// @Before("pt1()")
public void beforePrintLog(){
System.out.println("前置通知..........执行方法之前");
}
/**
* 后置通知
*/
// @AfterReturning("pt1()")
public void afterReturningPrintLog(){
System.out.println("后置通知..........执行方法结束");
}
/**
* 异常通知
*/
// @AfterThrowing("pt1()")
public void afterThrowingPrintLog(){
System.out.println("异常通知..........相当于cache里面的内容");
}
/**
* 最终通知
*/
// @After("pt1()")
public void afterPrintLog(){
System.out.println("最终通知..........相当于finally");
}
/**
* 环绕通知
* 问题:
* 当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了
* 分析:
* 通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。
* 解决:
* Spring框架为我们提供了一个接口,ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
* 该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
*
*/
@Around("pt1()")
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
System.out.println("..........前置通知");
//得到方法执行所需的参数
Object[] args = pjp.getArgs();
rtValue = pjp.proceed(args);
System.out.println("..........后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("..........异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("..........最终通知");
}
}
}
测试
package com.zjw.test;
import com.zjw.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试AOP的配置
*/
public class AOPTest {
public static void main(String[] args) {
//1、获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、获取对象
IAccountService accountService = (IAccountService)ac.getBean("accountService");
//3、执行方法
accountService.saveAccount();
}
}
结果
..........前置通知
saveAccount().....
..........异常通知
..........最终通知
Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
Spring 基于注解的AOP面向切面编程的更多相关文章
- Spring之注解实现aop(面向切面编程)
1:Aop(aspect object programming)面向切面编程,名词解释: 1.1:功能:让关注点代码与业务逻辑代码分离 1.2:关注点 重复代码就叫做关注点 ...
- 不依赖Spring使用AspectJ达到AOP面向切面编程
网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring注解式AOP面向切面编程.
1.AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式.aop底层是动态代理. package com.bie.config; import org.aspectj.lan ...
- 基于标注的AOP面向切面编程
1.什么是AOP Aspect Orientied Programming的简称,即 面向(方面)切面编程 ,不改变一个组件源代码的情况下 可以对组件功能进行增强. 例如:servlet中的过滤 ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- 基于SpringBoot AOP面向切面编程实现Redis分布式锁
基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
随机推荐
- oracle开启了审计功能,导致sysaux表空间满的问题
查询是否开启审计功能 SQL> show parameter audit 如下图所示: AUDIT_TRAIL参数用于指定数据库审计跟踪信息的记录方式.它接受三个可能的参数:NONE,DB,OS ...
- Handbook of Enumerative Combinatorics 阅读
Chapter 1 代数几何方法 Part1 代数方法 1.3 生成函数 符号化方法和拉格朗日反演 拆分数的生成函数和五边形数定理.斐波那契的拆分数 平面二叉树(Plane Binary Tree). ...
- jenkins全局工具配置
- 浅析Bootstrap中Tab(标签页)的使用方法
Bootstrap 导航元素使用相同的标记和基类,改变修饰的class,可以在不同的样式间进行切换如".nav-pills"(胶囊式导航)与 ".nav-tabs&quo ...
- Leetcode 236. 二叉树的最近公共祖先 & 235. 二叉搜索树的最近公共祖先(Python3)
236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x ...
- Flink学习(三) 批流版本的wordcount Scala版本
批处理代码: package com.wyh.wc import org.apache.flink.api.scala._ /** * 批处理代码 */ object WordCount { def ...
- luogu-P10596题解
简要题意 一个有 \(N\) 个元素的集合有 \(2N\) 个不同子集(包含空集),现在要在这 \(2N\) 个集合中取出若干集合(至少一个),使得它们的交集的元素个数为 \(K\),求取法的方案数, ...
- 【MATLAB习题】铰链四杆机构的运动学分析
铰链四杆机构题目&已知数据 matlab 代码 主程序文件: function main %输入已知数据 clear; i1=101.6; i2=254; i3=177.8; i4=304.8 ...
- mysql 卸载安装教程链接
https://blog.csdn.net/weixin_56952690/article/details/129678685 https://blog.51cto.com/u_16213646/70 ...
- allure 报告环境搭建
1.安装 pip install allure-pytest 2.下载allure 地址: https://repo.maven.apache.org/maven2/io/qameta/allure/ ...