关于代理:为什么 JDK 动态代理只能为接口生成代理?

导读:本篇文章讲解 关于代理:为什么 JDK 动态代理只能为接口生成代理?,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

原文链接:https://segmentfault.com/a/1190000021821314
在这里插入图片描述

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>5.0.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
    </dependencies>

在这里插入图片描述

.
└── com.mingrn.proxy
     ├── App.java
     ├── aop
     │   └── AspectProxy.java
     ├── config
     │   └── AppConfig.java
     └── service
         ├── UserService.java
         └── impl
             └── UserServiceImpl.java

在这里插入图片描述

public interface UserService {
    List find();
}

在这里插入图片描述

@Service("userService")
public class UserServiceImpl implements UserService {

    @Override
    public List find() {
        System.out.println("find");
        return null;
    }
}

在这里插入图片描述

@Configuration
@ComponentScan("com.mingrn.proxy")
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class AppConfig {
}

在这里插入图片描述

@Component
@Aspect
public class AspectProxy {

    @Pointcut("execution(* *com.mingrn.proxy.service.*.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before() {
        System.out.println("before");
    }
}

在这里插入图片描述

public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = (UserService) context.getBean("userService");
        userService.find();
    }
}

在这里插入图片描述

before
find

在这里插入图片描述

UserService userService = (UserService) context.getBean("userService");
System.out.println("userService instanceof UserServiceImpl ? " + (userService instanceof UserServiceImpl));

在这里插入图片描述

UserService userService = (UserService) context.getBean("userService");
System.out.println("userService instanceof Proxy ? " + (userService instanceof Proxy));

在这里插入图片描述

public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {}

在这里插入图片描述

public static byte[] generateProxyClass(final String var0, Class<?>[] var1, int var2){}

在这里插入图片描述

/**
 * Generate the specified proxy class.
 */
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces, accessFlags);

在这里插入图片描述

public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = (UserService) context.getBean("userService");
     
        Class<?>[] interfaces = new Class[]{UserService.class};
        byte[] bytes = ProxyGenerator.generateProxyClass("UserService", interfaces);

        File file = new File("/<path>/UserService.class");
        try {
            OutputStream outputStream = new FileOutputStream(file);
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

public final class UserService extends Proxy implements com.mingrn.proxy.service.UserService {
    private static Method m1;
    private static Method m3;
    private static Method m2;
    private static Method m0;

    public UserService(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        // ...
    }

    public final List find() throws  {
        // ...
    }

    public final String toString() throws  {
        // ...
    }

    public final int hashCode() throws  {
        // ...
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m3 = Class.forName("com.mingrn.proxy.service.UserService").getMethod("find");
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}

在这里插入图片描述

class UserService extends Proxy implements com.mingrn.proxy.service.UserService {}

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73814.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!