偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

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

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

1、AOP 核心概念

1.1 切面(Aspects)

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

1.2 連接點(diǎn)(Join Points)

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

1.3 通知(Advice)

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

1.4 切點(diǎn)(Pointcuts)

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

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

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

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

// 簡單的AOP實(shí)現(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)建了一個(gè)代理對象,該對象在每次方法調(diào)用前后執(zhí)行定義在 Advice接口中的操作。

3、字節(jié)碼操作

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

3.1 使用ASM或ByteBuddy

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

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

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

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

②、使用ByteBuddy來創(chuàng)建一個(gè)代理類,這個(gè)類在方法執(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)建了一個(gè)代理類,它覆蓋了toString方法。方法被調(diào)用時(shí),我們的LoggerInterceptor類將被調(diào)用。在LoggerInterceptor類中,我們在方法執(zhí)行前后添加了日志。

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

2019-05-10 10:50:04

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

2024-03-28 10:37:44

IoC依賴注入依賴查找

2023-12-19 09:24:22

LinuxBIOSUEFI

2024-02-20 14:10:55

系統(tǒng)緩存冗余

2021-01-06 08:34:21

Spring核心組件

2024-09-11 22:51:19

線程通訊Object

2023-11-20 10:09:59

2024-04-09 10:40:04

2024-01-19 14:03:59

Redis緩存系統(tǒng)Spring

2024-01-26 13:16:00

RabbitMQ延遲隊(duì)列docker

2023-09-26 07:49:11

AOP代理spring

2024-10-22 16:39:07

2024-07-26 08:10:10

2021-05-20 08:54:16

Go面向對象

2021-12-15 06:58:13

List 集合LinkedHashS

2024-02-04 10:08:34

2023-07-11 08:50:34

2021-04-30 20:25:20

Spring MVCJava代碼

2015-08-13 10:29:12

面試面試官

2023-02-16 08:10:40

死鎖線程
點(diǎn)贊
收藏

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