自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試官:拋開Spring來說,如何自己實現(xiàn)Spring AOP?

開發(fā) 前端
雖然Spring框架中的Spring AOP是Java社區(qū)中最著名的AOP實現(xiàn),但為了完全理解這種思想,我們可以不依賴Spring來實現(xiàn)AOP功能。

哈嘍,大家好,我是了不起。

作為一名Java程序員,面向切面編程這種編程思想,應該是我們?nèi)粘>幋a中常應用的編程思想。

這種編程范式,旨在提高代碼的模塊化程度。在AOP中,特定類型的問題被定義為“切面”,例如日志、事務管理或安全性等,這些切面可以在不改變核心業(yè)務邏輯的情況下,被插入程序的不同部分。對于提高代碼的優(yōu)雅,減少冗余度特別有用。

雖然Spring框架中的Spring AOP是Java社區(qū)中最著名的AOP實現(xiàn),但為了完全理解這種思想,我們可以不依賴Spring來實現(xiàn)AOP功能。

1、AOP 核心概念

1.1 切面(Aspects)

切面是AOP的核心,它將橫切關(guān)注點(如日志、事務處理等)與主業(yè)務邏輯分離。一個切面定義了何時(何處)和如何執(zhí)行這些橫切關(guān)注點。

1.2 連接點(Join Points)

連接點是應用執(zhí)行過程中能夠插入切面的點。在Java中,這通常是方法的調(diào)用。

1.3 通知(Advice)

通知定義了切面具體要執(zhí)行的操作。主要類型包括前置通知(before)、后置通知(after)、環(huán)繞通知(around)、拋出異常時通知(after throwing)和返回時通知(after returning)。

1.4 切點(Pointcuts)

切點定義了在哪些連接點執(zhí)行切面代碼。它是一組表達式,用于匹配特定的連接點。

2、使用Java動態(tài)代理

Java動態(tài)代理是一種在運行時創(chuàng)建代理對象的方法,代理對象可以在調(diào)用實際對象的方法前后執(zhí)行額外的操作。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

// 簡單的AOP實現(xiàn)
public class SimpleAOP {
    // 獲取代理對象
    public static Object getProxy(Object target, Advice advice) {
        return Proxy.newProxyInstance(
            target.getClass().getClassLoader(),
            target.getClass().getInterfaces(),
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    advice.beforeMethod(method);
                    Object result = method.invoke(target, args);
                    advice.afterMethod(method);
                    return result;
                }
            }
        );
    }

    // 通知接口
    public interface Advice {
        void beforeMethod(Method method);
        void afterMethod(Method method);
    }
}

在上述代碼中,getProxy 方法創(chuàng)建了一個代理對象,該對象在每次方法調(diào)用前后執(zhí)行定義在 Advice接口中的操作。

3、字節(jié)碼操作

字節(jié)碼操作是更高級但復雜的AOP實現(xiàn)方式。這涉及在類加載到JVM時修改其字節(jié)碼,插入額外的代碼。

3.1 使用ASM或ByteBuddy

  • ASM:一種低級字節(jié)碼操作庫,提供了對字節(jié)碼的細粒度控制。
  • ByteBuddy:相比ASM,ByteBuddy提供了更簡潔的API,適合那些不需要深入字節(jié)碼細節(jié)的場景。

下面我以 ByteBuddy 為例,展示一下如何使用ByteBuddy來實現(xiàn)一個基本的AOP功能:在方法執(zhí)行前后添加日志。

①、添加ByteBuddy依賴到你的項目中。如果你使用Maven,可以在pom.xml文件中加入以下依賴:

<dependency>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
    <version>1.11.22</version>
</dependency>

②、使用ByteBuddy來創(chuàng)建一個代理類,這個類在方法執(zhí)行前后打印日志:

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;

import java.lang.reflect.Modifier;

public class AOPExample {

    public static void main(String[] args) throws Exception {
        DynamicType.Unloaded<Object> dynamicType = new ByteBuddy()
            .subclass(Object.class)
            .method(ElementMatchers.named("toString"))
            .intercept(MethodDelegation.to(LoggerInterceptor.class))
            .make();

        Class<?> dynamicTypeLoaded = dynamicType
            .load(AOPExample.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();

        Object dynamicObject = dynamicTypeLoaded.newInstance();
        System.out.println(dynamicObject.toString());
    }

    public static class LoggerInterceptor {
        public static String intercept() {
            System.out.println("Method intercepted before execution");
            String result = "Hello from intercepted method";
            System.out.println("Method intercepted after execution");
            return result;
        }
    }
}

在上述代碼中,我們創(chuàng)建了一個代理類,它覆蓋了toString方法。方法被調(diào)用時,我們的LoggerInterceptor類將被調(diào)用。在LoggerInterceptor類中,我們在方法執(zhí)行前后添加了日志。

責任編輯:武曉燕 來源: Java技術(shù)指北
相關(guān)推薦

2019-05-10 10:50:04

Spring AOPJDK動態(tài)代理CGLIB動態(tài)代理

2024-03-28 10:37:44

IoC依賴注入依賴查找

2023-12-19 09:24:22

LinuxBIOSUEFI

2021-01-06 08:34:21

Spring核心組件

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2024-09-11 22:51:19

線程通訊Object

2023-11-20 10:09:59

2023-09-26 07:49:11

AOP代理spring

2024-01-19 14:03:59

Redis緩存系統(tǒng)Spring

2024-01-26 13:16:00

RabbitMQ延遲隊列docker

2024-04-09 10:40:04

2024-10-22 16:39:07

2024-07-26 08:10:10

2015-08-13 10:29:12

面試面試官

2021-04-30 20:25:20

Spring MVCJava代碼

2023-07-11 08:50:34

2020-06-22 08:50:27

Spring AOP代理

2021-05-20 08:54:16

Go面向對象

2021-12-15 06:58:13

List 集合LinkedHashS

2024-02-04 10:08:34

點贊
收藏

51CTO技術(shù)棧公眾號