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(){
}