当前位置: 首页 > 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();}}
}

测试结果

在这里插入图片描述

相关文章:

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(){}//B…...

Qt应用开发(基础篇)——按钮基类 QAbstractButton

一、前言 QAbstractButton类&#xff0c;继承于QWidget&#xff0c;是Qt按钮小部件的抽象基类&#xff0c;提供按钮常用的功能。 QAbstractButton按钮基类&#xff0c;它的子类(pushbutton、checkbox、toolbutton等)处理用户操作&#xff0c;并指定按钮的绘制方式。QAbstractBu…...

2023年最新的 前端面试题(个人总结)

目录 vue 1.vue2 和 vue3 的区别 2.vue2 和 vue3的原理 3.组合式api 和 选项式api 3. Proxy和object.defineproperty 4..v-show 与 v-if 的区别 5.计算属性和 watcher 6.虚拟DOM 7.key的作用是什么&#xff1f; 8.v-if 和 v-for 的优先级是什么&#xff1f; 9.vuex …...

服务器基本故障排查方法

1、加电类故障 定义 从上电(或复位)到自检完成这一段过程中电脑所发生的故障。可能的故障现象 1、 主机不能加电(如&#xff1a;电源风扇不转或转一下即停等)、有时不能加电、开机掉闸、机箱金属部分带电等; 2、 开机无显&#xff0c;开机报警; 3、 自检报错或死机、自检过程中…...

docker从零部署jenkins保姆级教程

jenkins&#xff0c;基本是最常用的持续集成工具。在实际的工作中&#xff0c;后端研发一般没有jenkins的操作权限&#xff0c;只有一些查看权限&#xff0c;但是我们的代码是经过这个工具构建出来部署到服务器的&#xff0c;所以我觉着有必要了解一下这个工具的搭建过程以及简…...

什么是 MVVM 模式?

MVVM 模式 官方解释&#xff1a;Vue 虽然没有完全遵循 MVVM 模型&#xff0c;但是 Vue 的设计也受到了它的启发。因此在文档中经常会使用 vm (ViewModel 的缩写) 这个变量名表示 Vue 实例。 什么是 MVVM 模式&#xff1f; MVVM 是一种新的开发模式&#xff0c;对比传统模式&…...

WebGL Varing变量的作用和内插过程,及执行Varing时涉及的图形装配、光栅化、颜色插值、片元着色器执行机制等详解

目录 前言 在 WebGL 或 OpenGL 中&#xff0c;“varying” 是一种用于在顶点着色器和片元着色器之间传递数据的特殊类型的变量。它允许在顶点着色器对数据进行处理后&#xff0c;在片元着色器中使用该处理后的数据进行进一步计算。 彩色三个点 ​编辑 彩色三个点示例代码…...

赢在起跑线:战略定位咨询带来的核心价值

在企业的发展之路上&#xff0c;三个核心问题始终伴随着我们&#xff1a;我们是谁?我们要做什么?我们要如何做&#xff1f;在业务的马拉松比赛中&#xff0c;开始时的位置至关重要。而战略定位咨询就是帮助企业赢在起跑线的关键。那么什么是战略定位&#xff1f;战略定位包含…...

【链表OJ 11】复制带随机指针的链表

前言: &#x1f4a5;&#x1f388;个人主页:​​​​​​Dream_Chaser&#xff5e; &#x1f388;&#x1f4a5; ✨✨刷题专栏:http://t.csdn.cn/UlvTc ⛳⛳本篇内容:力扣上链表OJ题目 目录 leetcode138. 复制带随机指针的链表 1. 问题描述 2.代码思路: 2.1拷贝节点插入到…...

Jenkins自动构建(Gitee)

Gitee简介安装JenkinsCLI https://blog.csdn.net/tongxin_tongmeng/article/details/132632743 安装Gitee jenkins-cli install-plugin gitee:1.2.7 # https://plugins.jenkins.io/gitee/releases获取安装命令(稍作变更) JenkinsURL Dashboard-->配置-->Jenkins Locatio…...

nginx离线安装

ngixn的离线安装(centos7) 需要的依赖 gcc、gcc-c pcre-8.42.tar.gz zlib-1.2.11.tar.gz openssl-1.1.1s.tar.gz perl-5.28.0.tar.gz 在进行nginx离线安装时&#xff0c;首先查看系统是否安装 gcc、gcc-c&#xff0c;若没有进行安装&#xff0c;请先进行安装 gcc -v #查…...

Oracle Merge Into ORA-00001: unique constaint violated问题

最近使用Datax同步进行定时数据同步&#xff0c;并在同步完之后进行回调sql进行统计操作。对应的ORACLE表结构如下&#xff1a; create table DATA_STAT_DAY ( DATA_DATE DATE, ID VARCHAR2(2), NAME VARCHAR2(2), CLASSNO VARCHAR2(2), SCORES NUMBER(16,0) );CREATE UNIQU…...

javaScript:DOM中的CSS操作

目录 1.style 属性获取元素写在行间的样式 2.getComputedStyle(元素对象&#xff0c;null)可以获取元素的非行间样式 3.案例&#xff08;定义一个div和按钮&#xff0c;每点击一次按钮div宽度增加&#xff09; 效果预览图 代码实现 在 JavaScript 中&#xff0c;可以通过…...

2023最新UI工作室官网个人主页源码/背景音乐/随机壁纸/一言

2023最新UI工作室官网个人主页源码/支持背景音乐/随机壁纸/一言 功能介绍&#xff1a; 载入动画 站点简介 Hitokoto 一言 日期及时间 实时天气 时光进度条 音乐播放器 移动端适配 打开文件&#xff1b;index.html和setting.json修改替换你的相关信息&a…...

常用命令之mysql命令之show命令

一、mysql show命令简介 mysql数据库中show命令是一个非常实用的命令&#xff0c;SHOW命令用于显示MySQL数据库中的信息。它可以用于显示数据库、表、列、索引和用户等各种对象的信息。我们常用的有show databases&#xff0c;show tables&#xff0c;show full processlist等&…...

iOS接入IJKPlayer遇到的问题汇总

这里有一个我自己编译的IJKMediaFramework&#xff0c;能解决目前Github上反馈很多常见的IJKPlayer使用问题(包含播放异常&#xff0c;UI主线程Crash等)&#xff0c;替换自己项目中的IJKMediaFramework即可链接: https://pan.baidu.com/s/1UO-YfN_1YIDOX81bgW8bag?pwdvq4u 提取…...

【LeetCode题目详解】第八章 贪心算法 part06 738.单调递增的数字 968.监控二叉树 (day37补)

本文章代码以c为例&#xff01; 一、力扣第738题&#xff1a;单调递增的数字 题目&#xff1a; 当且仅当每个相邻位数上的数字 x 和 y 满足 x < y 时&#xff0c;我们称这个整数是单调递增的。 给定一个整数 n &#xff0c;返回 小于或等于 n 的最大数字&#xff0c;且数…...

代码随想录算法训练营Day48 | 198.打家劫舍,213.打家劫舍II,337.打家劫舍III | Day 20 复习

198.打家劫舍 文章链接 | 题目链接 | 视频链接 C解法 class Solution { public:int rob(vector<int>& nums) {vector<int> dp (nums.size(), 0);if (nums.size() 0){return 0;}if (nums.size() 1){return nums[0];}dp[0] nums[0];dp[1] max(nums[0]…...

Spring Boot @Validated 和Javax的@Valid配合使用

一、Validated 和Valid有什么用 Validation 和Valid 常常配合使用对传输的参数进行数据校验的注解&#xff0c;并通过配置全局异常处理器进行合理化的提示&#xff0c;增加用户的体验 并且Validated可以通过分组来指定什么时候触发什么样的参数校验&#xff08;这里看一下就行…...

论文复现--lightweight-human-pose-estimation-3d-demo.pytorch(单视角多人3D实时动作捕捉DEMO)

分类&#xff1a;动作捕捉 github地址&#xff1a;https://github.com/Daniil-Osokin/lightweight-human-pose-estimation-3d-demo.pytorch 所需环境&#xff1a; Windows10&#xff0c;conda 4.13.0&#xff1b; 目录 conda环境配置安装Pytorch全家桶安装TensorRT&#xff08;…...

迷宫问题求解:从递归到队列的算法实战与性能对比

1. 迷宫问题与三种经典解法 迷宫问题就像我们小时候玩的走迷宫游戏&#xff0c;需要在错综复杂的路径中找到一条从起点到终点的通路。在计算机科学中&#xff0c;迷宫被抽象成一个二维矩阵&#xff0c;其中0代表可通行的路径&#xff0c;1代表障碍物。这个问题看似简单&#xf…...

IDEA插件开发:集成Nunchaku-flux-1-dev实现代码注释自动图解

IDEA插件开发&#xff1a;集成Nunchaku-flux-1-dev实现代码注释自动图解 1. 引言 作为一名Java开发者&#xff0c;你是否曾经面对过这样的困境&#xff1a;接手一个复杂的遗留系统&#xff0c;代码量庞大但注释稀少&#xff0c;逻辑关系错综复杂&#xff0c;光是理解代码执行…...

当仿真与FPGA打架时,你该信谁?

该文章同步至公众号OneChan 一、一个真实的故事&#xff1a;比特翻转的“罗生门” 去年&#xff0c;我们在做一款通信芯片的嵌入式固件开发。在仿真环境中&#xff0c;我们精心编写的DMA驱动完美无缺&#xff0c;数据传输的CRC校验次次通过。我们信心满满地把比特流下载到FPG…...

ROS2 Humble下,如何用MoveIt! Action接口让机械臂“听话”?一个抓取demo的完整复盘

ROS2 Humble下机械臂精准控制实战&#xff1a;从MoveIt! Action接口到完整抓取任务 在工业自动化和服务机器人领域&#xff0c;机械臂的精准运动控制一直是核心挑战。ROS2 Humble版本中的MoveIt!框架为这一挑战提供了优雅的解决方案&#xff0c;而理解其Action接口的运作机制则…...

Phi-3-mini-4k-instruct-gguf实操手册:短问答/改写/摘要三大高频场景落地

Phi-3-mini-4k-instruct-gguf实操手册&#xff1a;短问答/改写/摘要三大高频场景落地 1. 模型简介与核心能力 Phi-3-mini-4k-instruct-gguf是微软推出的轻量级文本生成模型&#xff0c;基于Phi-3系列优化而来。这个GGUF版本特别适合处理短文本任务&#xff0c;具有以下特点&a…...

RexUniNLU异常检测能力:识别虚假评论与垃圾内容

RexUniNLU异常检测能力&#xff1a;识别虚假评论与垃圾内容 1. 效果惊艳开场 打开任何一个内容平台&#xff0c;评论区总是最热闹的地方。但你可能不知道&#xff0c;每10条评论里&#xff0c;就有2-3条是机器生成的广告、水军刷的好评&#xff0c;或者是纯粹的垃圾信息。这些…...

3个创新方法:用Krita AI Diffusion插件实现智能动画制作

3个创新方法&#xff1a;用Krita AI Diffusion插件实现智能动画制作 【免费下载链接】krita-ai-diffusion Streamlined interface for generating images with AI in Krita. Inpaint and outpaint with optional text prompt, no tweaking required. 项目地址: https://gitco…...

客服机器人开放平台能自建知识库吗?以百应Agent为例,探讨成都企业售后自动解答的实现路径

在数字化转型加速的今天&#xff0c;成都作为西部电商和制造业重镇&#xff0c;众多企业面临售后咨询量激增的挑战。退货、物流追踪、产品故障排查等售后问题占客服咨询的 60% 以上&#xff0c;传统人工客服成本高、响应慢&#xff0c;已难以满足用户即时需求。客服机器人开放平…...

PyTorch内存优化实战:深入解析torch.utils.checkpoint的机制与应用

1. 为什么我们需要torch.utils.checkpoint&#xff1f; 第一次用PyTorch训练ResNet50时&#xff0c;我的16GB显存直接被撑爆了。当时怎么都想不明白——明明batch_size只设了32&#xff0c;怎么连这种经典模型都跑不动&#xff1f;后来才发现&#xff0c;问题出在前向传播时PyT…...

OpenAddresses多语言支持:全球地址数据的终极处理指南

OpenAddresses多语言支持&#xff1a;全球地址数据的终极处理指南 【免费下载链接】openaddresses A global repository of open address data. 项目地址: https://gitcode.com/gh_mirrors/op/openaddresses OpenAddresses是全球最大的开源地址数据仓库&#xff0c;提供…...