JDK 动态代理、Spring 的 AOP学习笔记

1、Spring 的 AOP

Spring 的 AOP 实现底层就是对动态代理的代码进行了封装,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强

2、JDK 的动态代理

2.1目标接口和目标类
public interface TargetInterface {
    public void save();
}

public class Target implements TargetInterface{
    public void save() {
        System.out.println("save running....");
    }
}
2.2增强类
public class Advice {
    public void before(){
        System.out.println("前置增强");
    }
    public void afterturnback(){
        System.out.println("后置增强");
    }
}
2.3动态代理测试
    public static void main(String[] args) {
        final Target target = new Target();
        final Advice advice = new Advice();
        //返回值就是动态生成的代理对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),//目标对象类加载器
                target.getClass().getInterfaces(),//目标对象相同的接口字节码对象数组
                new InvocationHandler() {
                    //调用代理对象的任何方法,实际上执行的都是invoke方法
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //前置增强
                        advice.before();
                        Object invoke = method.invoke(target, args);
                        //后置增强
                        advice.afterturnback();
                        return invoke;
                    }
                }
        );

        //调用代理对象的方法
        proxy.save();
    }

3、AOP 技术实现的内容和相关概念

Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

  • Target(目标对象):代理的目标对象

  • Proxy (代理):Spring产生的结果代理类

  • Joinpoint(连接点):指那些被拦截到的点

  • Pointcut(切入点):被增强的方法

  • Advice(通知/ 增强):封装增强业务逻辑的方法

  • Aspect(切面):切点+通知

  • Weaving(织入):将切点与通知结合的过程

4、基于 XML 的 AOP 开发

  1. 导入 AOP 相关坐标

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.3.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.13</version>
            </dependency>
    
  2. 创建目标接口和目标类、创建切面类

    //目标接口
    public interface TargetInterface {
        public void save();
    }
    //目标类
    public class Target implements TargetInterface{
        public void save() {
            System.out.println("save running....");
        }
    }
    //切面类
    public class Advice {
        public void before(){
            System.out.println("前置增强");
        }
        public void afterturnback(){
            System.out.println("后置增强");
        }
    }
    
  3. 将目标类和切面类的对象创建权交给 spring、导入aop命名空间、配置织入关系

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
    	<bean id="target" class="cn.guixinchn.proxy.Target"></bean>
    	<bean id="advice" class="cn.guixinchn.proxy.Advice"></bean>
    
        
        <aop:config>
            <!--引入advice切面类对象-->
            <aop:aspect ref="advice">
                <!--配置Target的save方法执行时要进行前置增强-->
                <aop:before method="before" pointcut="execution(public void cn.guixinchn.proxy.Target.save())"></aop:before>
                <!--配置Target的save方法执行时要进行后置增强-->
                <aop:after method="afterturnback" pointcut="execution(public void cn.guixinchn.proxy.Target.save())"></aop:after>
            </aop:aspect>
        </aop:config>
    
  4. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface targetInterface;

    @Test
    public void test(){
        targetInterface.save();
    }
}

4.1切点表达式的写法(作用:指定哪些方法需要增强)

execution([修饰符] 返回值类型 包名.类名.方法名(参数))

常用的写法:

execution(* cn.guixinchn..*.*(..))

修饰符可省略、* 标识任意 、 包名后面两个点…表示当前包及其子包下的类、方法里面两个…表示任意参数

4.2通知的类型
<aop:config>
    <aop:aspect ref=“切面类”>
        <aop:通知类型 method=“通知方法名称” pointcut=“切点表达式"></aop:通知类型>
    </aop:aspect>
</aop:config>
  • 前置通知:<aop:before>
  • 后置通知:<aop:after-returning>
  • 环绕通知:<aop:around>
  • 异常通知:<aop:throwing>
  • 最终通知:<aop:after>

5、基于注解的AOP开发

  1. 创建目标接口和目标类、创建切面类

    //目标接口
    public interface TargetInterface {
        public void save();
    }
    //目标类
    public class Target implements TargetInterface{
        public void save() {
            System.out.println("save running....");
        }
    }
    //切面类
    public class Advice {
        public void before(){
            System.out.println("前置增强");
        }
        public void afterturnback(){
            System.out.println("后置增强");
        }
    }
    
  2. 目标类和切面类的对象创建权交给 spring、切面类中使用注解配置织入关系

    //目标类
    @Component("target")
    public class Target implements TargetInterface{
        public void save() {
            System.out.println("save running....");
        }
    }
    
    //切面类
    @Component("advice")
    @Aspect
    public class Advice {
        @Before("execution(* cn.guixinchn..*.*(..))")
        public void before(){
            System.out.println("前置增强");
        }
        @AfterReturning("execution(* cn.guixinchn..*.*(..))")
        public void afterturnback(){
            System.out.println("后置增强");
        }
    }
    
    
  3. 在配置文件中开启组件扫描和 AOP 的自动代理

    <!--组件扫描-->
    <context:component-scan base-package="cn.guixinchn.proxy"></context:component-scan>
    <!--aop自动代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
  4. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface targetInterface;

    @Test
    public void test(){
        targetInterface.save();
    }
}
5.1注解通知的类型

通知的配置语法:@通知注解(“切点表达式")

  • 前置通知:@Before
  • 后置通知:@AfterReturning
  • 环绕通知:@Around
  • 异常通知:@AfterThrowing
  • 最终通知:@After
5.2注解aop开发步骤
  1. 目标类和切面类的对象创建权交给 spring
  2. 使用@Aspect标注切面类、配置织入关系、通知方法
  3. 在配置文件中开启组件扫描和 AOP 的自动代理<aop:aspectj-autoproxy/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值