【Spring实战】—— 13 AspectJ注解切面
前面了解了典型的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
@Aspect
public 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; @Component
public 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(""); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("time—— "+(end-start)+" millinseconds");
System.out.println("");
}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);
}
}
【Spring实战】—— 13 AspectJ注解切面的更多相关文章
- Spring实战4:面向切面编程
主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...
- Spring学习--用 ASpectJ 注解实现 AOP
用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...
- spring AOP 编程--AspectJ注解方式 (4)
1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...
- spring AOP编程--AspectJ注解方式
1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...
- [Spring实战笔记]4面向切面编程的Spring-代理
代理 代理(Proxy)是一种设计模式,可以在目标对象实现的基础上,扩展目标对象的功能. 代理对象是对目标对象的扩展,并会调用目标对象. 三种代理模式 静态代理 100% 代理对象与目标对象要实现相同 ...
- Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面
1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...
- Spring实战之切面编程
如果要重用通用功能的话,最常见的面向对象技术是继承(inheritance)或委托(delegation).但是,如果在整个应用中都使用相同的基类,继承往往会导致一个脆弱的对象体系:而使用委托可能需要 ...
- spring AOP (使用AspectJ的注解方式 的aop实现) (6)
目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
随机推荐
- codeforces 703D Mishka and Interesting sum 偶数亦或 离线+前缀树状数组
题目传送门 题目大意:给出n个数字,m次区间询问,每一次区间询问都是询问 l 到 r 之间出现次数为偶数的数 的亦或和. 思路:偶数个相同数字亦或得到0,奇数个亦或得到本身,那么如果把一段区间暴力亦或 ...
- Emit生成特定接口的类
参考 动态生成类 http://www w2bc com/Article/44799 http://www.cnblogs.com/yingql/archive/2009/03/24/1420914. ...
- Oracle 11g 新特性 --SQL Plan Management 说明
Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...
- 2019.3.28 JDBC相关
JDBC(Java Data Base Connectivity) JDBC是一组用Java编写的类和接口 使用JDBC的好处: 1.Java的开发人员完全不需要关心数据库的连接方式和实现手段 2.提 ...
- c#项目后期生成事件命令行常用命令
1. xcopy (复制文件) xcopy参数介绍 命令格式:XCOPY source [destination] 一堆可选的参数 参数介绍 source 指定要复制的文件. destinatio ...
- html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)
在上一篇中我简单写了个html自定义垂直导航菜单,缺点很明显,里面的数据是固定死的,不能动态更改数据. 这里我重写了一个修改版的垂直二级导航菜单,将原先的menuBox.init(config);修改 ...
- 学习javscript函数笔记(二)
定义: 函数包含一组语句,他们是JavaScript的基础模块单元,用于代码复用.信息隐藏和组合调用.函数用于指定对象的行为. 1.函数对象 JavaScript中的函数就是对象,函数对象连接到Fun ...
- es6 vs commonjs
'use strict' export function showMe() { alert("es6"); }; class logging { constructor() { a ...
- VIRTIO概述和基本原理
http://smilejay.com/2012/11/virtio-overview/ (KVM连载)5.1.1 VIRTIO概述和基本原理(KVM半虚拟化驱动) 11/15/2012MASTER ...
- Python 中的反射和自省
本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...