原文链接: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