Spring切面二使用注解
2024-10-19 07:37:33
package com.IC; public interface PhoneBiz { public void buyPhone(int num); //购买手机; public void salePhone(int num); //销售手机 }
package com.bean; import com.IC.*; public class PhoneBizImpl implements PhoneBiz { public void buyPhone(int num) { System.out.println("购买手机"+num); } public void salePhone(int num) { System.out.println("销售手机"+num); } }
package com.bean; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.jsp.tagext.TryCatchFinally; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class LogAspect { /*切入点*/ @Pointcut("execution(void *Phone(int))") public void p1(){} @Before("p1()") public void before(JoinPoint jp)throws Throwable{ Object[]args=jp.getArgs(); //目标方法所有参数; String methodName=jp.getSignature().getName(); //获得目标方法名称; if("buyPhone".equals(methodName)){ System.out.println(currentTime()+"即将执行进货操作,数量为:"+args[0]); } if("salePhone".equals(methodName)){ System.out.println(currentTime()+"即将执行销售操作,数量为:"+args[0]); } } @AfterReturning("p1()") public void afterReturing(JoinPoint jp)throws Throwable{ String methodName=jp.getSignature().getName(); if("buyPhone".equals(methodName)){ System.out.println(currentTime()+"进货完毕"); } if("salePhone".equals(methodName)){ System.out.println(currentTime()+"销售完毕"); } } /*环绕通知*/ @Around("p1()") public Object after(ProceedingJoinPoint pjp)throws Throwable{ String method=pjp.getSignature().getName(); long begin=System.currentTimeMillis(); System.out.println(currentTime()+":"+method+"方法开始执行,计时开始"); try { return pjp.proceed(); }finally{ long end=System.currentTimeMillis(); System.out.println(currentTime()+"耗时:"+(end-begin)+"毫秒"); } } private String currentTime() { SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); return sdf.format(new Date()); } }
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.IC.PhoneBiz; import com.bean.PhoneBizImpl; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); PhoneBiz pb=(PhoneBiz) ac.getBean("phoneBiz"); pb.buyPhone(100); pb.salePhone(40); } }
<?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:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 启用注解配置 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 目标业务对象 --> <bean id="phoneBiz" class="com.bean.PhoneBizImpl"></bean> <!-- 日志管理切面 --> <bean class="com.bean.LogAspect"></bean> </beans>
最新文章
- android 图像处理系列合集
- 用Python写一个简单的Web框架
- JS开发HTML5游戏《神奇的六边形》(一)
- Java 项目JDBC 链接数据库中会出现的错误
- Android组件Spinner使用
- HDU 3491 最小点权割集
- Android Bundle的使用
- mvn多模块开发消除重复依赖造成的打包失败
- android 项目中设置背景图片
- ASM上的备份集如何转移到文件系统中
- Redis笔记3-redis事务
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用FairyGUI (一)
- EditText搜索关键字,返回结果匹配关键字改变颜色
- django学习:一些疑惑
- Python mysql 创建连接
- 洛谷题解 P1138 【第k小整数】
- promql查询表达式
- Linux 启动过程详解
- 如何将U盘转化成NTFS格式
- GitLab 502问题的解决