spring

spring AOP

2019-11-01  本文已影响0人  love111

基本概念

官网参考https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop

AOP是什么

在面向OOP的主逻辑中会产生与主逻辑关系不大的横切性的问题,这些问题散落代码各个部分,难以维护。AOP的编程思想就是把这些问题和主业务逻辑分开,达到与主业务逻辑解耦的目的(例如登录的权限)。使代码的重用性和开发效率更高

应用场景

日志 权限 事务 效率检查 exception

术语

Aspect(切面) :切点,通知所在的类
Join point(连接点):目标对象中的方法
Pointcut(切点):某一类型连接点的集合
Advice(通知): 逻辑(做什么)、执行时机(之前或之后等)
target(目标对象) 被代理的对象 原始对象
proxy(代理对象) 包含了原始对象的代码和增加后的代码的那个对象
Weaving(织入):通知应用到切点的过程

实现AOP方式

1、spring AOP代理技术:cglib、JDK动态代理 动态编译在运行阶段生成代理对象
2、AspectJ 代理技术 编译阶段

实现Aspect支持步骤

1、开启@EnableAspectJAutoProxy支持
2、声明@Aspect切面类
3、在切面类书写切点和通知

示例代码

package com.aop.dao;
public interface IndexDaoInterface {
    public void query();
}

package com.aop.dao;
@Component
public class IndexDao implements IndexDaoInterface {
    public void query(){
        System.out.println("indexDao-----");
    }
}

package com.aop;
@Component
public class A {
    public void query(){
        System.out.println("---a-----");
    }
}

package com.aop;
@Component
@Aspect
public class MyAspectj {
//  @Pointcut("execution(* com.aop.A.*(..))")
//  @Pointcut("execution(* com.aop..*(..))")
//  @Pointcut(" within(com.aop.*)")
//  @Pointcut("args(java.lang.String,java.lang.String)")
    @Pointcut("this(com.aop.dao.IndexDao)")
    public void pointCutA(){
    }
    @Before("pointCutA()")
    public void before(){
        System.out.println("before");
    }
}


package com.aop;
@Configuration
@ComponentScan("com.aop")
@EnableAspectJAutoProxy
public class AppConfig {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class );
        A a = (A) ac.getBean("a");
        a.query();
                IndexDaoInterface bean = (IndexDaoInterface) ac.getBean("indexDao");
                bean.query();
    }
}

常用@Pointcut语法

可以用&&, || and ! 连接多个
@EnableAspectJAutoProxy(proxyTargetClass = true) 默认是false,则是用JDK动态代理。否则cglib
target:不会受到影响
this:代理对象会根据该选项的设置,判断能否找到对象,有影响

execution

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
throws-pattern?) -------------------
modifiers-pattern 如public/private/protected 可以省略
ret-type-pattern 表示返回类型 表示任意
declaring-type-pattern 方法所在类的全路径名
name-pattern 表示方法名
param-pattern 表示方法参数
throws-pattern 异常
execution(
com.aop.A.(..)) A对象所有方法
execution(
com.aop..(..)) 匹配aop包中任意类任意方法(不包括子包)
execution(* com.aop..*(..)) 匹配aop包及其子包中任意方法

within

within比execution相比,粒度更大,仅能实现到包和接口。类级别
execution可以精确到方法返回类型,参数个数,参数类型
within(com.aop.) 匹配aop包中任意方法(不包括子包)
within(com.aop..
) 匹配aop包及其子包中任意方法

args

args表达式的作用是匹配指定参数类型和指定参数数量的方法,与包类无关
args(java.lang.String) 参数类型String 且只有一个参数
类型的、参数个数和顺序需匹配

this

this表示代理对象
下面需要有接口实现IndexDaoInterface
@EnableAspectJAutoProxy(proxyTargetClass = false)
false----JDK动态代理 true-----cglib
@Pointcut("this(com.aop.dao.IndexDao)") proxyTargetClass = false JDK动态代理
此时代理对象由IndexDaoInterface 接口生成,不属于IndexDao类。AOP不会有输出
ac.getBean("indexDao")不用IndexDaoInterface接收将会报错
@Pointcut("this(com.aop.dao.IndexDao)") proxyTargetClass = true cglib
此时代理对象由继承IndexDao生成,属于IndexDao类。AOP正常有输出

target

target表示目标对象 proxyTargetClass 值的改变不会有影响
target(com.aop.dao.IndexDao) 目标对象属于IndexDao

带@

表示加了注解的
@target(com.anno.Xxxx) 目标对象中任意方法带有Xxxx注解

上一篇 下一篇

猜你喜欢

热点阅读