博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Spring实战】—— 13 AspectJ注解切面
阅读量:7050 次
发布时间:2019-06-28

本文共 3232 字,大约阅读时间需要 10 分钟。

前面了解了典型的AOP基于配置的使用方法,下面介绍下如何依赖于注解来实现AOP。

基于注解降低了配置文件的复杂程度,但是引入了程序间的耦合,其中的优劣待用户自己判断了。

需要注意的是,确定AspectJ与JDK之间的版本,否则会报错,。

  首先看一下基于注解的切面类,这时的切面不仅仅是一个POJO类了,与AOP进行了紧密的耦合。但是配置过程和方式都与原来的方式差不多。

package com.spring.test.chap44;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Component@Aspectpublic class Audience {    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(..))")    public void performance(){}        @Before("performance()")    public void takeSeats(){        System.out.println("takeSeats()");    }    @Before("performance()")    public void turnOffCellphones(){        System.out.println("turnOffCellphones()");    }    @AfterReturning("performance()")    public void applaud(){        System.out.println("applaud()");    }    @AfterThrowing("performance()")    public void demandRefund(){        System.out.println("demandRefund()");    }}

  接下来是其他一些必不可少的类:

  切点接口类:

package com.spring.test.chap44;public interface Performer {    public void perform();}

  切点实现类:

package com.spring.test.chap44;import org.springframework.stereotype.Component;@Componentpublic class Instrumentalist implements Performer{    public void perform() {        System.out.println("__________ perform ___________");    }}

  测试类:

package com.spring.test.chap44;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");                Performer performer = (Performer)ctx.getBean("xingoo");        performer.perform();    }}

  下面是重点的配置文件

  此时的配置文件注意要使spring知道哪一个是普通的bean,哪一个是通知。因此需要加上一个属性,保证AOP自动的识别通知。

  配置文件如下:

  执行结果如下:

turnOffCellphones()takeSeats()__________ perform ___________applaud()

 

  如果需要使用around只需要在切面中添加如下的代码就可以了:

@Around("performance()")    public void watchPerformance(ProceedingJoinPoint joinpoint){        try{            System.out.println("11111");                        long start = System.currentTimeMillis();                        joinpoint.proceed();                        long end = System.currentTimeMillis();                        System.out.println("time—— "+(end-start)+" millinseconds");            System.out.println("22222");        }catch(Throwable t){            System.out.println("in watchPerformance Throwable()");        }    }

   对于参数的传递的通知,也与原先通过配置的差不多

  在切面中配置好切点的方法,注意带上参数

private String str;    @Pointcut("execution(* com.spring.test.chap44.Instrumentalist.perform(String)) && args(str)")    public void performance(String str){}        @Before("performance(str)")    public void takeSeats(String str){        System.out.println("takeSeats()"+str);    }

  其他的基本都不用动了,只要把切点的方法,修改成带有参数的就可以了

public class Instrumentalist implements Performer{    public void perform(String str) {        System.out.println("__________ perform ___________" + str);    }}

 

本文转自博客园xingoo的博客,原文链接:,如需转载请自行联系原博主。
你可能感兴趣的文章
别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(5)
查看>>
ORA-01172、ORA-01151错误处理
查看>>
pl/sql 找到数组中公有的字符
查看>>
Spring MVC +MyBatis +MySQL 登录查询Demo 解决了mybatis异常【转】
查看>>
Junk Dimension
查看>>
win7下安装mysql-connector遇到的问题
查看>>
OSPF复习1
查看>>
恢复oracle数据库误更新
查看>>
ucenter整合心的
查看>>
回归测试
查看>>
eclipse+maven远程(自动)部署web项目到tomcat
查看>>
logrotate 介绍及使用
查看>>
chromium buildbot tree
查看>>
js禁止用户打开浏览器控制台或右键菜单查看源码
查看>>
Aspose.Words组件介绍及使用—基本介绍与DOM概述
查看>>
Bash Shell 中的特殊字符
查看>>
nginx proxy_set_header设置、自定义header
查看>>
Linux进程管理
查看>>
分离编译
查看>>
AngularJS 1.X 重点知识详解一
查看>>