当前位置: 首页 > news >正文

javaee spring aop 的五种通知方式

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.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--开启包的扫描 --><context:component-scan base-package="com.test" /><!-- 创建增强类对象 --><bean id="myAdvice" class="com.test.advice.MyAdvice" /><!-- 织入 --><aop:config><!-- 定义切点--><aop:pointcut id="pc" expression="execution(* com.test.service.impl.*.add(..))" /><!--切入 --><aop:aspect ref="myAdvice"><!-- 将增强类对象myAdvice中的before方法切入到pc对应的切点所在的方法前面 --><aop:before method="before" pointcut-ref="pc" /><aop:after method="after" pointcut-ref="pc" /><aop:around method="around" pointcut-ref="pc" /><aop:after-returning method="afterReturning" pointcut-ref="pc" /><aop:after-throwing method="afterThrowing" pointcut-ref="pc" /></aop:aspect></aop:config></beans>

切面类

package com.test.advice;import org.aspectj.lang.ProceedingJoinPoint;//增强类
public class MyAdvice {//将这个增强方法切入到service层的add方法前public void before(){System.out.println("添加用户之前");}//目标方法执行后(不管是出异常还是成功执行)public void after(){System.out.println("添加用户之后");}//环绕通知,用这个增强代码替换掉目标方法public void around(ProceedingJoinPoint point) throws Throwable {System.out.println("执行目标方法之前");point.proceed(); //放行切点处的方法(目标方法)}//目标方法成功执行后public void afterReturning(){System.out.println("目标方法成功执行后");}//目标方法出异常public void afterThrowing(){System.out.println("目标方法出异常以后才执行");}}

目标类

package com.test.service.impl;import com.test.service.IUsersService;
import org.springframework.stereotype.Service;@Service
public class UsersService implements IUsersService {@Overridepublic void add()  {System.out.println("添加用户...");}@Overridepublic void update() {System.out.println("修改用户...");}@Overridepublic void delete() {System.out.println("删除用户...");}
}

测试结果

在这里插入图片描述

依赖

<?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>org.example</groupId><artifactId>testSpring06</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>testSpring06 Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.18.Release</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.18.Release</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.18.Release</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.18.Release</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.18.RELEASE</version></dependency><!-- aop --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.10</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.18.RELEASE</version></dependency></dependencies><build><finalName>testSpring06</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
http://www.lryc.cn/news/160075.html

相关文章:

  • 【Redis】3、Redis主从复制、哨兵、集群
  • vcpkg方式安装zlmediakit
  • 【大数据】基于 Flink CDC 高效构建入湖通道
  • 微信小程序开发---网络数据请求
  • vulkan学习路径
  • NIFI使用InvokeHTTP发送http请求
  • Spire.xls+excel文件实现单据打印
  • win10系统配置vmware网络NAT模式
  • 什么是数据中台,关于数据中台的6问6答6方法
  • 什么是机器学习中的目标函数和优化算法,列举几种常见的优化算法
  • Edge被2345浏览器劫持 解决方法
  • uni-app:重置表单数据
  • 全球城市汇总【最新】
  • Java jvm 内存溢出分析
  • Qt CMake 中国象棋程序实现
  • ArcGIS 10.3安装教程!
  • Kafka的文件存储与稀疏索引机制
  • windowsxp下的mysql集群技术
  • 微信小程序开发---小程序的页面配置
  • 2023数学建模国赛B题完整论文来啦!(含一二问求解代码及三四问仿真模拟代码)
  • ERROR: your rosdep installation has not been initialized yet
  • python 文创产品商城推荐网上购物系统设计与实现vue
  • [lammps教程]OVITO绘制原子运动轨迹线
  • 2037:【例5.4】约瑟夫问题
  • 小节2:Python数学运算
  • 查看Oracle_表名、字段名、注释、进程及杀进程等常用语句
  • 吴恩达gradio课程:基于开源LLM(large language model)的聊天应用
  • Redis缓存预热、缓存雪崩、缓存击穿、缓存穿透
  • 手写Mybatis:第20章-Mybatis 框架源码10种设计模式分析
  • 系列一、前言