Spring AOP使用整理:使用@AspectJ风格的切面声明
要启用基于@AspectJ风格的切面声明,需要进行以下的配置:
<!-- 启用@AspectJ风格的切面声明 -->
<aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 通过注解定义bean。默认同时也通过注解自动注入 -->
<context:component-scan base-package="com.cjm"/>
基于@AspectJ风格的切面声明的源码:
/**
* 声明本类为一个切面
*/
@Component
@Aspect
public class MyAspectJ {
/**
* 声明一个切入点(包括切入点表达式和切入点签名)
*/
@Pointcut("execution(* com.cjm.model..*.*(..))")
public void pointcut1(){} /**
* 声明一个前置通知
*/
@Before("pointcut1()")
public void beforeAdvide(JoinPoint point){
System.out.println("触发了前置通知!");
} /**
* 声明一个后置通知
*/
@After("pointcut1()")
public void afterAdvie(JoinPoint point){
System.out.println("触发了后置通知,抛出异常也会被触发!");
} /**
* 声明一个返回后通知
*/
@AfterReturning(pointcut="pointcut1()", returning="ret")
public void afterReturningAdvice(JoinPoint point, Object ret){
System.out.println("触发了返回后通知,抛出异常时不被触发,返回值为:" + ret);
} /**
* 声明一个异常通知
*/
@AfterThrowing(pointcut="pointcut1()", throwing="throwing")
public void afterThrowsAdvice(JoinPoint point, RuntimeException throwing){
System.out.println("触发了异常通知,抛出了RuntimeException异常!");
} /**
* 声明一个环绕通知
*/
@Around("pointcut1()")
public Object aroundAdvice(ProceedingJoinPoint point)throws Throwable{
System.out.println("触发了环绕通知 开始");
Object o = point.proceed();
System.out.println("触发了环绕通知 结束");
return o;
}
}
1、切入点表达式的格式:execution([可见性] 返回类型 [声明类型].方法名(参数) [异常])
2、切入点表达式通配符: *:匹配所有字符 ..:一般用于匹配多个包,多个参数 +:表示类及其子类
3、切入点表达式支持逻辑运算符:&&、||、!
4、切入点表达式关键词:
1)execution:用于匹配子表达式。
//匹配com.cjm.model包及其子包中所有类中的所有方法,返回类型任意,方法参数任意 @Pointcut("execution(* com.cjm.model..*.*(..))") public void before(){}
2)within:用于匹配连接点所在的Java类或者包。
//匹配Person类中的所有方法
@Pointcut("within(com.cjm.model.Person)")
public void before(){}
//匹配com.cjm包及其子包中所有类中的所有方法
@Pointcut("within(com.cjm..*)")
public void before(){}
3) this:用于向通知方法中传入代理对象的引用。
@Before("before() && this(proxy)")
public void beforeAdvide(JoinPoint point, Object proxy){
//处理逻辑
}
4)target:用于向通知方法中传入目标对象的引用。
@Before("before() && target(target)
public void beforeAdvide(JoinPoint point, Object proxy){
//处理逻辑
}
5)args:用于将参数传入到通知方法中。
@Before("before() && args(age,username)")
public void beforeAdvide(JoinPoint point, int age, String username){
//处理逻辑
}
6)@within:用于匹配在类一级使用了参数确定的注解的类,其所有方法都将被匹配。
@Pointcut("@within(com.cjm.annotation.AdviceAnnotation)") - 所有被@AdviceAnnotation标注的类都将匹配
public void before(){}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Inherited
public @interface AdviceAnnotation { }
7)@target:和@within的功能类似,但必须要指定注解接口的保留策略为RUNTIME。
@Pointcut("@target(com.cjm.annotation.AdviceAnnotation)")
public void before(){}
8)@args:传入连接点的对象对应的Java类必须被@args指定的Annotation注解标注。
@Before("@args(com.cjm.annotation.AdviceAnnotation)")
public void beforeAdvide(JoinPoint point){
//处理逻辑
}
public class Person {
public void say(Address address){
//处理逻辑
}
}
@AdviceAnnotation
public class Address {
}
如果需要在Person类的say方法被调用时触发beforeAdvide通知,那么say方法的参数对应的Java类型Address类必须要被@AdviceAnnotation标注。
9)@annotation:匹配连接点被它参数指定的Annotation注解的方法。也就是说,所有被指定注解标注的方法都将匹配。
@Pointcut("@annotation(com.cjm.annotation.AdviceAnnotation)")
public void before(){}
public class Person {
@AdviceAnnotation
public void say(Address address){
//处理逻辑
}
}
Person类的say方法被@AdviceAnnotation标注,所以它匹配。
10)bean:通过受管Bean的名字来限定连接点所在的Bean。该关键词是Spring2.5新增的。
@Pointcut("bean(person)")
public void before(){}
id为person的受管Bean中的所有方法都将匹配。
Spring AOP使用整理:使用@AspectJ风格的切面声明的更多相关文章
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- spring AOP 之三:使用@AspectJ定义切入点
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- Spring AOP 知识整理
通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...
- Spring AOP基于配置文件的面向方法的切面
Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 ...
- spring aop在mvc的controller中加入切面无效
spring aop在mvc的controller中加入切面无效 因为MVC的controller,aop默认使用jdk代理.要使用cglib代理. 在spring-mybatis.xml配置文件中加 ...
- Spring AOP使用整理:各种通知类型的介绍
2.PersonImpl类的源码 public class PersonImpl implements Person { private String name; private int age; p ...
- Spring AOP使用整理:自动代理以及AOP命令空间
三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...
- Spring AOP—注解配置方法的使用
Spring除了支持Schema方式配置AOP,还支持注解方式:使用@AspectJ风格的切面声明. 1 启用对@AspectJ的支持 Spring默认不支持@AspectJ风格的切面声明,为了支持需 ...
- Spring AOP支持的AspectJ切入点语法大全
原文出处:http://jinnianshilongnian.iteye.com/blog/1420691 Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的, ...
随机推荐
- Xamarin 的 MVVM 之 Caliburn.Micro
约定 Caliburn.Micro 以下简称 CMXamarin.Form 以下简称 XF 摘要CM 当前已释出 3.0 beta 版https://github.com/Caliburn-Micro ...
- 一种仿照Asp.net Mvc思维构建WebSocket服务器的方法
问题场景 Asp.net Mvc提供了DependencyResolver.Routing.Filter. Modelbinder等webForm所没有新概念,提高Web服务编写的便利性,记得很久之前 ...
- jQuery ajax - get(),getJSON(),post()方法
1) jQuery ajax - get() 方法: $(selector).get(url,data,success(response,status,xhr),dataType) 参数 ...
- maven integration with eclipse 3.0.4 does not work with NTLM proxy
Recently downloaded m2e(maven integration with eclipse). The version is 3.0.4. My environment is beh ...
- 解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题
4.40版源码,找到SourceGrid/SourceGrid/Common/CustomScrollControl.cs 这个文件 定位 CustomScrollWheel() 方法,把if条件判断 ...
- js中模仿接口继承
一般情况下我们会这样写,但是这样写的话,不够美化或者直观. 如果我们可以这样写的话,感觉更好: 但是样子的话,我们没有考虑原型覆盖之类的,因为我们通常的情况,我们继承只有一层,在通常情况下,我们原型覆 ...
- 1、面向对象以及winform的简单运用(开篇)
面向对象概述: 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 面向对象,首先要有一个对象,那么对象是什么呢? 对象的定义是人们要进行研 ...
- JavaScript事件---事件对象
发文不易,若转载传播,请亲注明出处,谢谢! 内容提纲: 1.事件对象 2.鼠标事件 3.键盘事件 4.W3C与IE JavaScript事件的一个重要方面是它们拥有一些相对一致的特点,可以给你的开 ...
- 第十三课:js操作节点的创建
浏览器提供了多种方法创建节点.比如:document.createElement,innerHTML,insertAdjacentHTML,createContextualFragment. docu ...
- 【HDU 2604】Queuing
题 题意 f和m两种字母组成字符串,fmf 和 fff 这种为不安全的字符串,现在有2*L个字母,问你有多少安全的字符串.答案mod M. 分析 递推,这题本意是要用矩阵快速幂.不过我发现这题好神奇, ...