1. package com.IC;
  2.  
  3. public interface PhoneBiz {
  4. public void buyPhone(int num); //购买手机;
  5. public void salePhone(int num); //销售手机
  6. }
  1. package com.bean;
  2.  
  3. import com.IC.*;
  4.  
  5. public class PhoneBizImpl implements PhoneBiz {
  6.  
  7. public void buyPhone(int num) {
  8. System.out.println("购买手机"+num);
  9. }
  10.  
  11. public void salePhone(int num) {
  12. System.out.println("销售手机"+num);
  13. }
  14. }

  1. package com.bean;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5.  
  6. import javax.servlet.jsp.tagext.TryCatchFinally;
  7.  
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.ProceedingJoinPoint;
  10. import org.aspectj.lang.annotation.AfterReturning;
  11. import org.aspectj.lang.annotation.Around;
  12. import org.aspectj.lang.annotation.Aspect;
  13. import org.aspectj.lang.annotation.Before;
  14. import org.aspectj.lang.annotation.Pointcut;
  15. @Aspect
  16. public class LogAspect {
  17. /*切入点*/
  18. @Pointcut("execution(void *Phone(int))")
  19. public void p1(){}
  20.  
  21. @Before("p1()")
  22. public void before(JoinPoint jp)throws Throwable{
  23. Object[]args=jp.getArgs(); //目标方法所有参数;
  24. String methodName=jp.getSignature().getName(); //获得目标方法名称;
  25.  
  26. if("buyPhone".equals(methodName)){
  27. System.out.println(currentTime()+"即将执行进货操作,数量为:"+args[0]);
  28. }
  29. if("salePhone".equals(methodName)){
  30. System.out.println(currentTime()+"即将执行销售操作,数量为:"+args[0]);
  31. }
  32. }
  33. @AfterReturning("p1()")
  34. public void afterReturing(JoinPoint jp)throws Throwable{
  35. String methodName=jp.getSignature().getName();
  36. if("buyPhone".equals(methodName)){
  37. System.out.println(currentTime()+"进货完毕");
  38. }
  39. if("salePhone".equals(methodName)){
  40. System.out.println(currentTime()+"销售完毕");
  41. }
  42. }
  43. /*环绕通知*/
  44. @Around("p1()")
  45. public Object after(ProceedingJoinPoint pjp)throws Throwable{
  46. String method=pjp.getSignature().getName();
  47. long begin=System.currentTimeMillis();
  48. System.out.println(currentTime()+":"+method+"方法开始执行,计时开始");
  49. try {
  50. return pjp.proceed();
  51. }finally{
  52. long end=System.currentTimeMillis();
  53. System.out.println(currentTime()+"耗时:"+(end-begin)+"毫秒");
  54. }
  55. }
  56. private String currentTime() {
  57. SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
  58. return sdf.format(new Date());
  59. }
  60. }

  1. package com.test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import com.IC.PhoneBiz;
  7. import com.bean.PhoneBizImpl;
  8.  
  9. public class Test {
  10. public static void main(String[] args) {
  11. ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  12. PhoneBiz pb=(PhoneBiz) ac.getBean("phoneBiz");
  13. pb.buyPhone(100);
  14. pb.salePhone(40);
  15. }
  16. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  13.  
  14. <!-- 启用注解配置 -->
  15. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  16. <!-- 目标业务对象 -->
  17. <bean id="phoneBiz" class="com.bean.PhoneBizImpl"></bean>
  18. <!-- 日志管理切面 -->
  19. <bean class="com.bean.LogAspect"></bean>
  20. </beans>

Spring切面二使用注解的更多相关文章

  1. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  2. spring boot(二):注解大全

    spring boot注解 @Autowired 注解的意思就是,当Spring发现@Autowired注解时,将自动在代码上下文中找到和其匹配(默认是类型匹配)的Bean,并自动注入到相应的地方去. ...

  3. spring入门(二) 使用注解代替xml配置

    1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  4. Spring AOP(二)--注解方式

    本文介绍通过注解@AspectJ实现Spring AOP,这里要重点说明一下这种方式实现时所需的包,因为Aspect是第三方提供的,不包含在spring中,所以不能只导入spring-aop的包,为了 ...

  5. spring aop使用,spring aop注解,Spring切面编程

    ================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...

  6. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  7. Spring AOP之使用注解创建切面

    上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...

  8. SpringInAction--面向切片的Spring以及如何使用注解创建切面

    什么叫做切片..什么叫做AOP... 与大多数技术一样,AOP已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point). (一大串书上的 ...

  9. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

随机推荐

  1. 自己通过Cygwin编译的windowsx86下的更新至4.0.1

    采用方法:https://my.oschina.net/maxid/blog/186506 方法中在3.2.6未找到src/redis.h文件 未修改 方法中 /deps/hiredis/net.c ...

  2. 「日常训练」The Necklace(UVA-10054)

    代码 for(int i=0; i!=n; ++i) { int u = cin.nextInt(); int v = cin.nextInt(); edges.add(new Edge(u,v)); ...

  3. windows下如何将Python文件打包成.exe可执行文件

    在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电脑后就没法运行了.这显得很不方便,不符合我们的初衷,那么有没 ...

  4. unittest,requests——接口测试脚本及报告

    用unittest管理两个利用requests模块,做百度搜索的简单接口测试用例,之后自动输出报告 # encoding=utf-8import requests,unittest,HTMLTestR ...

  5. Jupyter 安装并配置工作路径[转]

    1.通过python的pip方式安装jupyterpython和pip都安装好后,通过cmd进入命令提示窗口,找到python安装目录下的Script目录,例如我的是路径是:C:\Program Fi ...

  6. Android 8.0 NavigationBar 颜色问题。

    1. packages/SystemUI/src/com/android/systemui/statusbar/phone/LightBarController.java public void on ...

  7. TW实习日记:第十天

    今天任务很简单,就是出品项目的时间轴显示页面和动态路由设置.其实时间轴页面很快就做完了,在做完处理完数据之后,然而有很多细节需要打磨,这就又考验了我面向搜索引擎编程的能力,根据需求百度了很多css的样 ...

  8. POJ 1417 并查集 dp

    After having drifted about in a small boat for a couple of days, Akira Crusoe Maeda was finally cast ...

  9. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  10. TCP系列41—拥塞控制—4、Linux中的慢启动和拥塞避免(一)

    一.Linux中的慢启动和拥塞避免 Linux中采用了Google论文的建议把IW初始化成了10了.在linux中一般有三种场景会触发慢启动过程 1.连接初始建立发送数据的时候,此时cwnd初始化为1 ...