首页 新闻 赞助 找找看

关于jdk 动态代理 如果InvocationHandler中用静态方法有没有什么隐患

0
悬赏园豆:20 [已解决问题] 解决于 2018-12-12 15:48

class MyInvocationHandler implements InvocationHandler{
private static Service targetService;

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return null;
}

public static Service getProxyService(Service target){
    targetService = target;
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    Class<?>[] interfaces = targetService.getClass().getInterfaces();
    return (Service)Proxy.newProxyInstance(contextClassLoader,interfaces,new MyInvocationHandler());
}

}

不爱吃蘑菇的大蘑菇的主页 不爱吃蘑菇的大蘑菇 | 初学一级 | 园豆:83
提问于:2018-12-12 11:16
< >
分享
最佳答案
0

还是直接用匿名内部类吧
Service proxyInstance = (Service) Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("add".equals(method.getName())) {
System.out.println("HELLOIDEA");
}
return null;
}
});
proxyInstance.add();

不爱吃蘑菇的大蘑菇 | 初学一级 |园豆:83 | 2018-12-12 15:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册