【Spring实战】—— 13 AspectJ注解切面
来源:互联网 发布:js控制视频进度条 编辑:程序博客网 时间:2024/05/05 02:11
前面了解了典型的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自动的识别通知。
<aop:aspectj-autoproxy proxy-target-class="true"/>
配置文件如下:
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="xingoo" class="com.spring.test.chap44.Instrumentalist"/> <bean id="audience" class="com.spring.test.chap44.Audience" /> <aop:aspectj-autoproxy proxy-target-class="true"/></beans>
执行结果如下:
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); }}
0 0
- 【Spring实战】—— 13 AspectJ注解切面
- Spring AOP(三)——通过@AspectJ注解切面
- Spring——AOP(面向切面编程)@AspectJ注解方式
- spring aop, aspectj 注解声明切面
- 用 AspectJ 注解声明Spring切面
- spring-aop---AspectJ注解式切面编程。
- 面向切面的 Spring —— 如何注入 AspectJ 切面?
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- Spring AOP与AspectJ 面向切面编程配置与注解
- Spring支持的AspectJ注解式切面编程
- [Spring实战系列](18)注解切面
- Spring实战-注解切面(五)
- Spring aspectJ切面使用步骤
- Spring AOP切面编程 AspectJ
- AOP-面向切面编程——AspectJ
- Spring实战——面向切面的Spring
- Spring实战——面向切面的Spring
- Spring学习笔记 —— AOP(面向切面编程) 之AspectJ
- 第6章 深入理解ActivityManagerService
- Annotation
- 跑偏程序员自学cocos2d-x之一 创建工程
- 外观模式
- wxWidgets学习笔记(5)- wxTextCtrl
- 【Spring实战】—— 13 AspectJ注解切面
- android开发 用线程进行耗时操作,统一处理 第二版
- 机器学习入门----以阿里移动推荐算法大赛为例
- long long超时了,而换成int变成ac了
- so汉化心得
- java多线程问题(上)
- C# struct的陷阱:无法修改“...”的返回值,因为它不是变量
- 理解Thread.Sleep()
- Android中Java和JavaScript交互