【AOP 随笔】

pom 注解
socpe 指定的范围

Proxy 代理

AOP
ClassLoader classLoader = 接口.class.getClassLoader();
Class<?>interfaces = new Class[]{接口.class};
InvocationHandler handler = 实现 InvocationHandler(),重写元方法。

//o就是代理类
Object o = Proxy.newProxyInstance(classLoader ,interfaces , handler );

JDk 动态代理,必须保证被代理的类实现了接口,才能用。

cglib 就不需要。

AOP就进行了兼容,有接口用jdk代理。没用接口用cglib代理。
动态选着动态代理的方式。

AOP-----------
切面: 关注点模块化
连接点: 切面和被增强方法的连接点
通知:
切点:切入地点。

begin()
after()
afterException()
afterEnd()

pom依赖
aspectjweaver
spring-aspects

注解
@Aspect 声明切面
@Component

@Before(“execution(*.com…**(…))”)

<aop:aspectj-auto

什么是AOP
JDK动态代理和CGLIB动态代理的区别
如何理解Spring中的代理
解释一下Spring里面的几个名词
execution: 可以匹配到方法
在这里插入图片描述

返回值:如果jdk自带的可以不写完整的限定名;自定义的要写完整额限定名。
包名:cn.* 只能匹配一级
cn…* 可以匹配到子孙包

类名:
方法名:
参数: … 点点代码任意参数,否则,要写具体;

within: 只能匹配到类
within(cn.abcdef.service.impl.*)

annotation: 匹配注解

JoinPoint的使用:

@Before(“execution(* cn.xx.service..(…))”)
publlic void before(JoinPoint joinPoint){
String methodName= joinPpint.getSignature().getName();
Object[] atgs = joinPpint.getArgs();
//可以记录方法名和参数
}

// 后置异常通知
@AfterThrowing(value=“pointcut()”,
throwing=“ex”)
public void afterThrowing(Exception ex){
//异常栈固定写法—begin
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw, true));
//异常栈固定写法----end
System.out.println(“后置异常通知”+sw.getBuffer().toString());
}
// 后置返回通知
@AfterReturning(value=“pointcut()”,
returning = “returnValue”)
public void afterReturning(Object returnValue){
System.out.println(“返回值:”+returnValue);
}

声明一个切点,其他地方引用
@Pointcut(“execution(* cn.thuiyaogo.service.impl..(…))”)
public void pointcut(){
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值