package com.ailianshuo.springaop.sample03;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* 通知类,横切逻辑
* @author ailianshuo
* 2017年7月25日 下午5:21:42
*
* @Component表示该类的实例会被Spring IOC容器管理;
* @Aspect表示声明一个切面;
* @Before表示before为前置通知,通过参数execution声明一个切点
*
* execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
*
* @AspectJ使用AspectJ专门的切点表达式描述切面,Spring所支持的AspectJ表达式可分为四类:
* 方法切点函数:通过描述目标类方法信息定义连接点。
* 方法参数切点函数:通过描述目标类方法入参信息定义连接点。
* 目标类切点函数:通过描述目标类类型信息定义连接点。
* 代理类切点函数:通过描述代理类信息定义连接点。
* 常见的AspectJ表达式函数:
* execution():满足匹配模式字符串的所有目标类方法的连接点
* @annotation():任何标注了指定注解的目标方法链接点
* args():目标类方法运行时参数的类型指定连接点
* @args():目标类方法参数中是否有指定特定注解的连接点
* within():匹配指定的包的所有连接点
* target():匹配指定目标类的所有方法
* @within():匹配目标对象拥有指定注解的类的所有方法
* @target():匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解
* this():匹配当前AOP代理对象类型的所有执行方法
*
*
* 优先执行顺序:
* annotation --> args --> execution --> this --> within
*
*
*
*
*/
@Component
@Aspect
public class Advices {
/**
* 定义一个切点然后复用
*/
@Pointcut("execution(* com.ailianshuo.springaop.sample03.Math.*(..))")
public void pointcutMath(){
}
@Before("pointcutMath()")
public void before(JoinPoint jp){
//System.out.println(jp.getSignature().getName());
System.out.println("----------before advice----------");
}
/**
* execution():满足匹配模式字符串的所有目标类方法的连接点
* com.ailianshuo.springaop.sample03.Math包下所有类的所有方法被切入
* @param jp
@After("pointcutMath()")
public void after_execution(JoinPoint jp){
System.out.println("----------after execution advice----------");
}
*/
/**
* within():匹配指定的包的所有连接点
* com.ailianshuo.springaop.sample03包下所有类的所有方法被切入
* @param jp
@After("within(com.ailianshuo.springaop.sample03.*)")
public void after_within(JoinPoint jp){
System.out.println("----------after within advice----------");
}
*/
/**
*
* this切点函数
* this():匹配当前AOP代理对象类型的所有执行方法
*
* @param jp
@After("this(com.ailianshuo.springaop.sample03.Math)")
public void after_this_Math(JoinPoint jp){
System.out.println("----------after this Math advice----------");
}
*/
/**
*
* this切点函数
* this():匹配当前AOP代理对象类型的所有执行方法
*
* @param jp
@After("this(com.ailianshuo.springaop.sample03.StrUtil)")
public void after_this_StrUtil(JoinPoint jp){
System.out.println("----------after this StrUtil advice----------");
}
*/
/**
*
* args切点函数
* 要求方法有两个int类型的参考才会被织入横切逻辑
* @param jp
@After("args(int,int)")
public void after_args(JoinPoint jp){
System.out.println("----------after args advice----------");
}
*/
/**
*
* @annotation切点函数
* @annotation():任何标注了指定注解的目标方法链接点
* 要求方法必须被注解com.ailianshuo.springaop.sample03.MyAnno才会被织入横切逻辑
* @param jp
*/
@After("@annotation(com.ailianshuo.springaop.sample03.MyAnno)")
public void after_annotation(JoinPoint jp){
System.out.println("----------after annotation advice----------");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springaop.zip (47个子文件)
springaop
pom.xml 1KB
target
test-classes
com
ailianshuo
springaop
classes
aopsample04.xml 1KB
META-INF
maven
com.ailianshuo
springaop
pom.xml 1KB
pom.properties 217B
MANIFEST.MF 115B
aopsample01.xml 1KB
aopsample02.xml 1KB
aopsample03.xml 1KB
com
ailianshuo
springaop
sample01
Test.class 1KB
Math.class 1KB
Advices.class 953B
sample04
Test.class 2KB
StrUtil.class 649B
Math.class 2KB
Advices.class 3KB
sample03
Test.class 2KB
StrUtil.class 813B
MyAnno.class 439B
Math.class 2KB
Advices.class 1KB
sample02
Test.class 1KB
Math.class 1KB
Advices.class 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.core.resources.prefs 119B
org.eclipse.jdt.core.prefs 243B
src
test
java
com
ailianshuo
springaop
main
java
aopsample04.xml 1KB
aopsample01.xml 1KB
aopsample02.xml 1KB
aopsample03.xml 1KB
com
ailianshuo
springaop
sample01
Math.java 808B
Advices.java 490B
Test.java 644B
sample04
StrUtil.java 232B
Math.java 1KB
Advices.java 2KB
Test.java 2KB
sample03
StrUtil.java 328B
Math.java 1KB
Advices.java 4KB
Test.java 1KB
MyAnno.java 677B
sample02
Math.java 1KB
Advices.java 1KB
Test.java 644B
.project 561B
.classpath 1022B
共 47 条
- 1
资源评论
ai_lian_shuo
- 粉丝: 21
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功