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

javaee spring aop 注解实现

切面类

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

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:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

依赖

<?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>testSpring07</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>testSpring07 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>testSpring07</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>

目标类

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("删除用户...");}
}

测试类

package com.test.aop;import com.test.service.IUsersService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAop {@Testpublic void test(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");IUsersService usersServiceProxy=  applicationContext.getBean("usersService",IUsersService.class);try {usersServiceProxy.add();} catch (Exception e) {e.printStackTrace();}}
}

测试结果

在这里插入图片描述

http://www.lryc.cn/news/156053.html

相关文章:

  • Qt应用开发(基础篇)——按钮基类 QAbstractButton
  • 2023年最新的 前端面试题(个人总结)
  • 服务器基本故障排查方法
  • docker从零部署jenkins保姆级教程
  • 什么是 MVVM 模式?
  • WebGL Varing变量的作用和内插过程,及执行Varing时涉及的图形装配、光栅化、颜色插值、片元着色器执行机制等详解
  • 赢在起跑线:战略定位咨询带来的核心价值
  • 【链表OJ 11】复制带随机指针的链表
  • Jenkins自动构建(Gitee)
  • nginx离线安装
  • Oracle Merge Into ORA-00001: unique constaint violated问题
  • javaScript:DOM中的CSS操作
  • 2023最新UI工作室官网个人主页源码/背景音乐/随机壁纸/一言
  • 常用命令之mysql命令之show命令
  • iOS接入IJKPlayer遇到的问题汇总
  • 【LeetCode题目详解】第八章 贪心算法 part06 738.单调递增的数字 968.监控二叉树 (day37补)
  • 代码随想录算法训练营Day48 | 198.打家劫舍,213.打家劫舍II,337.打家劫舍III | Day 20 复习
  • Spring Boot @Validated 和Javax的@Valid配合使用
  • 论文复现--lightweight-human-pose-estimation-3d-demo.pytorch(单视角多人3D实时动作捕捉DEMO)
  • 在Windows下设置将EXE开机自启动
  • 反序列化漏洞及漏洞复现
  • 软件工程笔记001
  • java进行系统的限流实现--Guava RateLimiter、简单计数、滑窗计数、信号量、令牌桶
  • 《86盒应用于家居中控》——实现智能家居的灵动掌控
  • 【LeetCode】328. 奇偶链表
  • 数字城市:科技革命下的未来之城
  • Qt鼠标点击事件处理:按Escape键退出程序
  • P1162 填涂颜色
  • Vagrant命令
  • vue3+pinia实现动态类名及动态颜色