About proxy: Why can JDK dynamic proxy only generate proxy for interface?

Original link: https://segmentfault.com/a/1190000021821314
Insert picture description here

    <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>

Insert picture description here

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

Insert picture description here

public interface UserService {
    
    
    List find();
}

Insert picture description here

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

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

Insert picture description here

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

Insert picture description here

@Component
@Aspect
public class AspectProxy {
    
    

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

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

Insert picture description here

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

Insert picture description here

before
find

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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

Insert picture description here

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();
        }
    }
}

Insert picture description here

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());
        }
    }
}

Insert picture description here

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

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_28807077/article/details/111327627