# java.lang.ClassCastException：类com.sun.proxy.$Proxy0无法转换为类com.example.MyInterface（com.sun.proxy.$Proxy0在加载器'app'的模块java.base中；com.example.MyInterface在加载器'app'的未命名模块中）

- **ID:** `java/classcastexception-proxy-invocationhandler`
- **领域:** java
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

当为代理的类加载器不可见的接口创建JDK动态代理（Proxy.newProxyInstance）时发生，通常因为接口由不同的类加载器加载或代理类来自不同模块。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Spring Framework 5.3.x | active | — | — |
| Spring Framework 6.x | active | — | — |

## 解决方案

1. ```
   Ensure the interface is loaded by the same class loader as the proxy: `ClassLoader commonLoader = Thread.currentThread().getContextClassLoader(); MyInterface proxy = (MyInterface) Proxy.newProxyInstance(commonLoader, new Class[]{MyInterface.class}, handler);`
   ```
2. ```
   For Spring applications, configure proxy-target-class="true" to use CGLIB proxies instead of JDK dynamic proxies: `<aop:aspectj-autoproxy proxy-target-class="true"/>` or `@EnableAspectJAutoProxy(proxyTargetClass = true)`.
   ```
3. ```
   Move the interface definition to a shared library jar that is loaded by the common class loader (e.g., the bootstrap or extension class loader) to avoid class loader isolation.
   ```

## 无效尝试

- **Casting the proxy to the concrete class instead of the interface** — JDK dynamic proxies only implement interfaces, not concrete classes; casting to a concrete class will always fail. (95% 失败率)
- **Adding the interface to the classpath multiple times in different jars** — Multiple class loaders loading the same interface cause type identity issues; the proxy implements one version, but the cast expects another. (70% 失败率)
- **Using CGLIB proxies instead without changing the interface visibility** — CGLIB creates subclass proxies, which also require the target class to be accessible; the root cause (class loader mismatch) remains. (60% 失败率)
