Spring-AOP的JDK动态代理的局限性

前言今天在做一个业务需求的时候,目标是为服务接口的实现类定义切面来解耦某些与业务无关的操作。期间突然萌生了一个想法:我们知道spring-aop在被通知类拥有的接口的情况下默认采用JDK动态代理模式来织入切面的,而JDK动态代理只会对接口中定义的方法进行拦截,那么被代理中自定义的方法在调用时会发生什么行为呢? 我的初步设想是Spring必然会给出异常提示,因为你这样做很容易忽略掉未在接口中方...
摘要由CSDN通过智能技术生成

前言

一个很常规的需求,目标是为服务接口的实现类定义切面来解耦某些与业务无关的操作。期间突然萌生了一个想法:我们知道spring-aop在被通知类拥有的接口的情况下默认采用JDK动态代理模式来织入切面的,而JDK动态代理只会对接口中定义的方法进行拦截,那么被代理中自定义的方法在调用时会发生什么行为呢?

被代理类的接口

package com.special.learningspring.aspect;

public interface Performance {
   

    String perform();
}

接口实现类

package com.special.learningspring.aspect;

import com.special.learningspring.aspect.Performance;
import org.springframework.stereotype.Component;

/**
 * Created by Special on 2018/6/12 17:51
 */
@Component
public class ImplementsPer implements Performance{
   

    public String perform() {
        String str = "2323";
        return str + "0000";
    }

    public String myPerform() {
        return "my sucess";
    }
}

切面

package com.special.learningspring.aspect;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created by Special on 2018/6/12 11:52
 */
@Aspect
@Component
public class Audience {
   

    //Pointcut定义可重用的切点
    @Pointcut("execution (* com.special.learningspring.aspect.ImplementsPer.perform(..))")
    public void performance() {}

    @Before("performance()")
    public void silenceCellPhones() {
        System.out.println("Silence cell phones");
    }

}

测试类


import com.special.learningspring.aspect.ImplementsPer;
import com.special.learningspring.aspect.Performance;
import com.special.learningspring.config.PerformanceConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by Special on 2018/6/10 15:13
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PerformanceConfig.class)
public class AspectXMLTest {
   

    @Autowired
    ImplementsPer implementsPer;

    @Test
    public void test() {
        System.out.println(implementsPer.perform());
    }
}

报错

    D:\program-model-path\jdk1.8\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\software\IDEAProfessional\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar=49825:D:\software\IDEAProfessional\IntelliJ IDEA 2018.1.6\bin" -Dfile.encoding=UTF-8 -classpath "D:\software\IDEAProfessional\IntelliJ IDEA 2018.1.6\lib\idea_rt.jar
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP中的JDK动态代理是一种实现动态代理的方式。它通过使用JDK的Proxy类和InvocationHandler接口来生成代理对象和处理代理逻辑。具体来说,当我们使用Spring AOP时,代理对象的生成直接使用了JDK动态代理的Proxy.newProxyInstance方法。而代理逻辑则是通过实现了InvocationHandler接口的invoke方法来实现的。在Spring AOP中,JdkDynamicAopProxy类实现了InvocationHandler接口,并完成了Spring AOP拦截器链拦截等一系列逻辑。通过JdkDynamicAopProxy的invoke方法,我们可以在目标方法执行前后添加额外的逻辑。这种方式的优点是可以在运行时动态地生成代理对象,不需要事先编写代理类的代码。这样可以更加灵活地实现横切关注点的功能,并且可以避免代码冗余。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring AOP --JDK动态代理方式](https://blog.csdn.net/m0_46195271/article/details/108714116)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringAOP JDK动态代理](https://blog.csdn.net/weixin_46281472/article/details/125629339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值