AOP 动态添加函数

Function.prototype.before = function(beforefn) {
    
    // 保存原函数的引用
    var self = this;
    
    // 返回包含了原函数和新函数的代理函数
    return function() {

        // 执行新函数,修正this
        beforefn.apply(this, arguments);

        // 执行原函数
        return self.apply(this, arguments);
    };
};

Function.prototype.after = function (afterfn) {
    var self = this;
    return function () {

        // 先调用 调用的函数,在调用afterfn
        // 调用这个函数的也是一个函数
        var ret = self.apply(this, arguments);
        afterfn.apply(this, arguments);
        return ret;
    };
};

var func = function () {
    console.log(2);
};


// 每次使用之前都要这么写一遍
function init(fn) {
    fn = fn.before(function () {
        console.log(1);
    })
    .after(function () {
        console.log(3);
    });    

    return fn;
}

func = init(func);

func();
View Code

 

转载于:https://www.cnblogs.com/tujw/p/10502724.html

### Spring AOP 动态代理的实现原理 Spring AOP 的核心在于动态代理技术,它允许在运行时生成代理对象并将其织入目标对象中。这种机制使得可以在不修改原有业务逻辑的情况下,向其添加额外的功能。 #### 1. **JDK 动态代理** JDK 动态代理基于接口实现,适用于目标类实现了至少一个接口的情况。其实现主要依赖于 `java.lang.reflect.Proxy` 类和 `InvocationHandler` 接口。 以下是 JDK 动态代理的核心流程: - 创建一个实现了 `InvocationHandler` 接口的处理器类。 - 使用 `Proxy.newProxyInstance()` 方法生成代理实例[^2]。 代码示例如下: ```java import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class JdkDynamicProxyExample { public static void main(String[] args) { MyService target = new MyServiceImpl(); MyService proxyInstance = (MyService) Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before method execution"); Object result = method.invoke(target, args); System.out.println("After method execution"); return result; } }); proxyInstance.execute(); } interface MyService { void execute(); } static class MyServiceImpl implements MyService { @Override public void execute() { System.out.println("Executing service..."); } } } ``` #### 2. **CGLIB 动态代理** 当目标类未实现任何接口时,Spring AOP 将采用 CGLIB 动态代理。CGLIB 是一种字节码操作框架,能够在运行时扩展 Java 类[^3]。 以下是 CGLIB 动态代理的关键步骤: - 自定义一个继承自 `MethodInterceptor` 的拦截器类。 - 利用 `Enhancer` 类创建代理实例。 代码示例如下: ```java import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CglibDynamicProxyExample { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(MyClass.class); // 设置父类 enhancer.setCallback(new MethodInterceptor() { // 设置回调函数 @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("Before method execution"); Object result = proxy.invokeSuper(obj, args); System.out.println("After method execution"); return result; } }); MyClass proxyInstance = (MyClass) enhancer.create(); // 创建代理实例 proxyInstance.perform(); } static class MyClass { public void perform() { System.out.println("Performing action..."); } } } ``` #### 3. **Spring AOP 中的选择机制** Spring AOP 默认优先使用 JDK 动态代理。如果目标类没有实现接口,则会自动切换至 CGLIB 动态代理[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值