Spring AOP的底层实现

Spring AOP的层实现

AOP的底层实现有两种:JDK动态代理和CGLIB动态代理,其中JDK动态代理要求必须实现了接口。
Spring在运行期生成动态代理,不需要特殊的编译器。
若目标对象实现了接口Spring使用JDK的java.lang.reflect.Proxy类代理,若没有实现接口,使用CGLIB库生成目标对象的子类。
应优先使用对接口创建代理,便于程序解耦维护。
标记为final的方法,不能被代理,因为无法进行覆盖:

  • JDK动态代理,是针对接口生成子类,接口中的方法不能使用final修饰;
  • CGLib是针对目标类产生子类,因此类或方法不能使用final。

Spring只支持方法连接点,不提供属性连接点。

详细案例

目录结构

spring_aop
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - demo1
- - - - - - UserDao1.java
- - - - - - UserDao1Imp.java
- - - - - - JdkProxy.java
- - - - - demo2
- - - - - - UserDao2.java
- - - - - - CglibProxy.java
- - test
- - - java
- - - - MyTest.java
- pom.xml

具体文件

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>11maven.compiler.source>
        <maven.compiler.target>11maven.compiler.target>
    properties>

    <groupId>com.zhangxin9727groupId>
    <artifactId>spring_aopartifactId>
    <version>1.0-SNAPSHOTversion>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <version>5.4.0version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>5.1.4.RELEASEversion>
        dependency>
    dependencies>
project>

UserDao1.java

package com.zhangxin9727.demo1;

public interface UserDao1 {
    void save();

    void update();

    void delete();

    void find();
}

UserDao1Imp.java

package com.zhangxin9727.demo1;

public class UserDao1Impl implements UserDao1 {
    @Override
    public void save() {
        System.out.println("保存···");
    }

    @Override
    public void update() {
        System.out.println("修改···");
    }

    @Override
    public void delete() {
        System.out.println("删除···");
    }

    @Override
    public void find() {
        System.out.println("查询···");
    }
}

JdkProxy.java

package com.zhangxin9727.demo1;

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

//代理类,对save方法进行增强
public class JdkProxy implements InvocationHandler {
    private UserDao1 userDao1;

    public JdkProxy(UserDao1 userDao1) {
        this.userDao1 = userDao1;
    }

    public UserDao1 createProxy() {
        UserDao1 proxy = (UserDao1) Proxy.newProxyInstance(userDao1.getClass().getClassLoader(), userDao1.getClass().getInterfaces(), this);
        return proxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("save".equals(method.getName())) {
            System.out.println("···权限校验···");
            return method.invoke(userDao1, args);
        }
        return method.invoke(userDao1, args);
    }
}

UserDao2.java

package com.zhangxin9727.demo2;

public class UserDao2 {
    public void save() {
        System.out.println("保存···");
    }

    public void update() {
        System.out.println("修改···");
    }

    public void delete() {
        System.out.println("删除···");
    }

    public void find() {
        System.out.println("查询···");
    }
}

CglibProxy.java

package com.zhangxin9727.demo2;

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 CglibProxy implements MethodInterceptor {
    UserDao2 userDao2;

    public CglibProxy(UserDao2 userDao2) {
        this.userDao2 = userDao2;
    }

    public UserDao2 createProxy() {
        //创建核心类
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(userDao2.getClass());
        //设置回调
        enhancer.setCallback(this);
        //生成代理
        UserDao2 proxy = (UserDao2) enhancer.create();
        return proxy;
    }

    @Override
    public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if ("save".equals(method.getName())) {
            System.out.println("···权限校验···");
        }
        return methodProxy.invokeSuper(proxy, objects);
    }
}

MyTest.java

import com.zhangxin9727.demo1.UserDao1;
import com.zhangxin9727.demo1.UserDao1Impl;
import com.zhangxin9727.demo1.JdkProxy;

import com.zhangxin9727.demo2.CglibProxy;
import com.zhangxin9727.demo2.UserDao2;
import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    public void test1() {
        UserDao1 userDao1 = new JdkProxy(new UserDao1Impl()).createProxy();
        userDao1.update();
        userDao1.save();
        userDao1.delete();
        userDao1.find();
    }

    @Test
    public void test2() {
        UserDao2 userDao2 = new CglibProxy(new UserDao2()).createProxy();
        userDao2.update();
        userDao2.save();
        userDao2.delete();
        userDao2.find();
    }
}

你可能感兴趣的:(SSM)