基于@Aspect的AOP配置
来源:互联网 发布:淘宝店铺入口图 编辑:程序博客网 时间:2024/05/01 07:28
1、Spring除了支持Schema方式配置AOP,还支持注解方式:使用@Aspect来配置
2、Spring默认不支持@Aspect风格的切面声明,通过如下配置开启@Aspect支持:
Java代码
<aop:aspectj-autoproxy/>
3、通过以上配置,Spring就能发现用@Aspect注解的切面内并把它应用到目标对象上。
4、定义一个切面:
Java代码
@Aspect
public class AspectStyle {
@Before("execution(* com.sxit..*.*(..))")
public void before(){
System.out.println("方法执行前执行.....");
}
}
5、后置返回通知:
Java代码
@AfterReturning("execution(* com.sxit..*.*(..))")
public void afterReturning(){
System.out.println("方法执行完执行.....");
}
6、后置异常通知:
Java代码
@AfterThrowing("execution(* com.sxit..*.*(..))")
public void throwss(){
System.out.println("方法异常时执行.....");
}
7、后置最终通知:
Java代码
@After("execution(* com.sxit..*.*(..))")
public void after(){
System.out.println("方法最后执行.....");
}
8、环绕通知:
Java代码
@Around("execution(* com.sxit..*.*(..))")
public Object around(ProceedingJoinPoint pjp){
System.out.println("方法环绕start.....");
try {
pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("方法环绕end.....");
}
9、按上面的每一个通知都要写一个定义,其实这部分可以抽出来,定义个一个公共的切入点。
Java代码
package com.sxit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectStyle {
@Pointcut("execution(* com.sxit..*.*(..))")
public void init(){
}
@Before(value="init()")
public void before(){
System.out.println("方法执行前执行.....");
}
@AfterReturning(value="init()")
public void afterReturning(){
System.out.println("方法执行完执行.....");
}
@AfterThrowing(value="init()")
public void throwss(){
System.out.println("方法异常时执行.....");
}
@After(value="init()")
public void after(){
System.out.println("方法最后执行.....");
}
@Around(value="init()")
public Object around(ProceedingJoinPoint pjp){
System.out.println("方法环绕start.....");
Object o = null;
try {
o = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("方法环绕end.....");
return o;
}
}
10、打印信息:
Java代码
方法before前执行.....
方法环绕start.....
我看.....................
方法after执行.....
方法环绕end.....
方法afterReurning执行.....
参考自:http://jinnianshilongnian.iteye.com/blog/1418598
http://ch-space.iteye.com/blog/493956
0 0
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP配置
- 基于@Aspect的AOP注解配置
- 基于@Aspect的AOP
- Spring框架 基于@Aspect的AOP配置(六)
- 第三章 AOP 基于@Aspect的AOP
- 基于注解@Aspect的AOP实现
- Spring -- AOP入门基础&基于Aspect的AOP通知用法
- Spring AOP 的@Aspect
- 基于Schema配置的Aspect Oriented programming
- spring 基于Aspect和注解的切面编程(aop)
- Aop之Xml aspect配置
- Java多线程应用之火车站售票
- VS添加lib以及对编译过程的总结
- 常州测试day2:flower
- Python学习之路,每日一记
- ASCII码对照表
- 基于@Aspect的AOP配置
- 各类链接(外部链接、内部链接……)的使用方法合集
- FTP上传文件 SocketException: Connection reset
- [LeetCode]--327. Count of Range Sum && Merge_Sort && Fenwick Tree
- python学习笔记(1)之如何在python3.x下安装PIL、numpy以及matplotlib
- oracle连接总结(内连接、外连接、自然连接,交叉连接,自连接)
- PHP安全编程之暴力破解攻击
- spring aop出现0 can't find referenced pointcut mymethod
- POJ 1218 The Drunk Jailer