JDK及CGLIB动态代理-AOP4种增强

本文深入探讨了AOP(面向切面编程)的概念及其实现方式,包括JDK动态代理与CGLIB两种底层技术原理,并通过示例代码详细解释了如何在Java中运用这两种技术实现动态代理。此外,还介绍了四种不同的增强类型:前置增强、后置增强、环绕增强及异常增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态代理 AOP底层实现:有接口自动应用的就是JDK动态代理
(1).JDK 在运行时运行时注入
本质:在内存中构建出接口的实现类
特点:被代理对象,必须有接口

实例:

复制代码
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Test {
    public static void main(String[] args) {
      final SomeService service = new SomeServiceImpl();

      SomeService proxy =(SomeService)Proxy.newProxyInstance(service.getClass().getClassLoader(),
              service.getClass().getInterfaces(), new InvocationHandler() {
                  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                      System.out.println("before=======");
                      method.invoke(service,args);
System.out.println("after========"); return null; } }); proxy.doSome(); } }
复制代码

Cglib 底层,注入,编译期已经注入了
本质:在内存中生成被代理类(目标类)的【子类】
特点:可以在没有接口的情况下代理
对于不使用接口的业务类,无法使用JDK动态代理,cglib采用非常底层的字节码技术,
可以为一个类创建子类

复制代码
import cn.SomeServiceImpl;
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 Test {
    public static void main(String[] args) {
        final SomeServiceImpl someService = new SomeServiceImpl();

        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(someService.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("=======before");
                methodProxy.invoke(someService,objects);
                return null;
            }
        });
        SomeServiceImpl proxy = (SomeServiceImpl) enhancer.create();
        proxy.doSome();
    }
}
复制代码

 

接下来是四种增强

前置增强 MethodBeforeAdvice

复制代码
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("after========");
    }
}
复制代码
复制代码
<bean id="service" class="cn.SomeServiceImpl"></bean>
<bean id="beforeAdvice" class="cn.BeforeAdvice"></bean> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="service"></property> <property name="interceptorNames" value="beforeAdvice"></property> </bean>
复制代码

 

后置增强 AfterReturingAdvice

复制代码
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("after========");
    }
}
复制代码

xml文件内容

复制代码
   <bean id="service" class="cn.SomeServiceImpl"></bean>

    <bean id="afterAdvice" class="cn.AfterAdvice"></bean>

    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="afterAdvice"></property>
    </bean>
复制代码

3.环绕增强

复制代码
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HrAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before====");
        methodInvocation.proceed();
        System.out.println("after====");
        return null;
    }
}
复制代码

xml文件

复制代码
   <bean id="service" class="cn.SomeServiceImpl"></bean>

    <bean id="hrAdvice" class="cn.HrAdvice"></bean>

    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="hrAdvice"></property>
    </bean>
复制代码

异常增强

public class MyThrowsAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception e){
        System.out.println("网络异常......");
    }
}
复制代码
   <bean id="service" class="cn.SomeServiceImpl"></bean>

    <bean id="throwsAdvice" class="cn.MyThrowsAdvice"></bean>

    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="service"></property>
        <property name="interceptorNames" value="throwsAdvice"></property>
    </bean>

转载于:https://www.cnblogs.com/buai/p/8587800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值