【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 ...
随机推荐
- bzoj1041 圆上的整点 数学
题目传送门 题目大意:求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 思路:没思路,看大佬的博客(转载自https://blog.csdn.net/csyzcyj),转载只 ...
- HDU - 1085 母函数
年轻人的第一道母函数入门题 #include<bits/stdc++.h> using namespace std; const int maxn = 1000+2000+5000+1; ...
- layer.open中content里面的元素追加click事件,触发不了
[注意] 事件要追加在触发弹出事件的点击事件里面 $('#feedback').click(function(){ layer.open({ content:'<div><div c ...
- [转] javascript 判断对象是否存在的10种方法总结
[From] http://www.jb51.net/article/44726.htm Javascript语言的设计不够严谨,很多地方一不小心就会出错.举例来说,请考虑以下情况.现在,我们要判断一 ...
- linux驱动之设备模型
linux 设备驱动模型 inux2.6提供了新的设备模型:总线.驱动.设备.基本关系简要的概括如下: 驱动核心可以注册多种类型的总线. 每种总线下面可以挂载许多设备.(通过kset devices) ...
- mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)
1.使用相同索引键值的冲突 由于mysql 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但如果是使用相同的索引键,是会出现锁冲突的.设计时要注意 例如:city表city_ ...
- jquery 属性选择器
jquery 属性选择器 第一种根据属性选择E[attr] $("[title]").click().......... 即选择所有元素内 属性带有title的元素即<l ...
- Git~GitLab当它是一个源代码管理工具时
最近开始接触和使用GitLab,用它来做源代码的版本控制,CI.CD持续集成和持续交付,感觉功能确实很强大,今天也只能先说一下它的源代码管理功能,核心就是GIT,对GIT进行了封装,提供了一些扩展功能 ...
- [linux]解决DNS配置重启丢失
DNS配置重启丢失 每次重启后都修改DNS配置文件 /etc/resolv.conf从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvconf/resolv.conf ...
- 触发Full GC的时机
由于Full GC的耗时是Minor GC的十倍左右,所以Full GC的频率设计得比Minor GC低得多.现总结一下触发Full GC的情况. 在那些实现了CMS的比较新的虚拟机中,如果配置了-X ...