Original link: 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 {
}