最近在编码的时候用到了回调模版,想到了JDK的动态代理在用起来并不怎么方便,如果将其改造成AOP式的方式就能在使用起来方便的多。
package com.fjx.proxy.up.test;
import com.fjx.proxy.up.MethodInterceptor;
import com.fjx.proxy.up.MethodInvocation;
import com.fjx.proxy.up.ProxyUp;
/**
* 动态代理升级类测试
* @author FANGJINXIN
*
*/
public class ProxyUpTest {
public static void main(String[] args) {
UserMgr userMgr = new UserMgrImpl();
Object proxy = ProxyUp.newProxy(userMgr, new MethodInterceptor(){
public void invoke(MethodInvocation invocation) {
if(invocation.getMethod().getName().equals("saveUser")){
System.out.println("开始记录保存日志");
invocation.process();
System.out.println("保存成功日志记录完毕");
}else{
invocation.process();
}
}});
userMgr = (UserMgr)proxy;
userMgr.saveUser("湘");
userMgr.sayHello("湘");
}
}
这样用户只要关系对代理的类的那些方法进行处理了,很像spring的aop
其他代码如下:
package com.fjx.proxy.up;
import java.lang.reflect.Proxy;
/**
* 动态代理
*
* @author FANGJINXIN
*
*/
public abstract class ProxyUp {
public static Object newProxy(Object target,MethodInterceptor methodInterceptor){
MethodInvocation invocation = new MethodInvocation(target,methodInterceptor);
Object obj = Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
invocation);
return obj;
}
}
package com.fjx.proxy.up;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 方法回调类
* @author FANGJINXIN
*
*/
public class MethodInvocation implements InvocationHandler{
private Object target;//目标类
private Method method;//回调的方法
private Object[] args;//方法参数
private MethodInterceptor methodInterceptor;//方法拦截对象
public MethodInvocation(Object target, MethodInterceptor methodInterceptor){
this.target = target;
this.methodInterceptor = methodInterceptor;
}
public MethodInvocation() {
}
/**
* 调用目标对象的原始方法
*/
public void process(){
try {
method.invoke(target, args);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
/**
* 重写代理回调方法
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
this.method = method;
this.args = args;
methodInterceptor.invoke(this);//调用方法拦截器代理方法
return proxy;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public MethodInterceptor getMethodInterceptor() {
return methodInterceptor;
}
}
package com.fjx.proxy.up;
/**
* 方法回调接口
* 用户通过实现拦截器来动态代理原始对象
*
* @author FANGJINXIN
*
*/
public interface MethodInterceptor {
/**
* 方法代理类
* @param invocation
*/
public void invoke(MethodInvocation invocation);
}
UserMgr自己去写这样的实现会让你想起什么呢?希望大家指点
分享到:
相关推荐
JDK 1.4相较于后续版本,缺乏一些现代化的特性,如泛型、枚举、动态代理等。这些特性在2.4.1版本的FCKeditor-java中可能被使用,导致在JDK 1.4环境下编译失败或运行异常。 在着手修改源码前,我们需要对FCKeditor-...
模块 --with-http_gzip_static_module --with-openssl=/opt/soft/openssl-1.1.1g 更改为 ./configure --prefix=/usr/local/nginx --...同时,持续学习和掌握Linux、openssl、jdk等相关知识,对于运维人员来说至关重要。
* 动态代理升级类测试 * @author FANGJINXIN * */ public class ProxyUpTest { public static void main(String[] args) { UserMgr userMgr = new UserMgrImpl(); Object proxy = ProxyUp.newProxy...
Kotlin 版本 玩安卓 Flutter 版本玩安卓 Java版本玩Android小程序版本玩安卓 Kotlin 版本玩安卓 Jetpack 版本截图 首页 体系 导航 项目 帖子 搜索感谢工具类RxJava改造韋克韋斯特滑行横幅垂直标签布局代理网布拉瓦...
- Spring使用JDK动态代理或者CGLIB动态代理技术来创建代理对象。 - 当目标对象实现了一个或多个接口时,Spring会使用JDK动态代理;如果目标对象没有实现任何接口,则使用CGLIB动态代理。 3. **配置示例:** - ...
面向切面编程(AOP)允许在不修改源代码的情况下添加额外功能,Spring通过动态代理实现,包括JDK代理和CGLIB代理。 以上只是简要解答,每个话题都值得深入研究,以充分理解其背后的原理和技术细节。作为架构师,...
当Spring无法通过传统的Java代理(JDK Proxy)对无接口类进行代理时,就会转向CGLIB。CGLIB通过创建目标类的子类并覆盖其方法来实现动态代理,即使目标类没有实现任何接口。 首先,我们需要理解Spring拦截器的基本...
动态代理机制,如CGLib和JDKProxy,也被详细讨论,帮助读者理解如何在运行时动态生成代理对象,实现对目标对象的功能增强。 ### Java平台下的Web开发 随着互联网的发展,Web开发成为了软件开发的重要领域。本书...
在软件环境方面,OSChina 使用的是RedHat Enterprise Linux 5.3操作系统,搭配Sun JDK 1.6.0,Nginx 0.8.x作为反向代理服务器,Tomcat 7.x作为应用服务器,以及Percona Server 5.1(MySQL的一个衍生版本)作为数据库...
代理方负责定期报告指标数据。 3.支持主流服务器平台的安装和部署,例如Linux,Windows,Solaris,AIX,HP-UX等。 4.在安装包中(注意:不在仓库中),单击下面的下载链接以下载安装包。 下载后,将其解压缩。 在...
- **JDKProxy**:Java内置的一种代理机制,适用于实现了接口的类。 #### Java平台下的Web开发 - **标记语言**:用于描述网页结构的语言,如HTML。 - **自定义标记库的开发**: - **Tag接口的生命周期**:描述了...
- **服务器统一规划与配置**:课程中提供了三台CentOS 6虚拟机的具体配置步骤,包括JDK版本统一、hosts文件设置、防火墙管理等。 - **Maven安装与配置**:介绍了Maven的基本安装步骤和环境变量配置方法。 通过以上...
如果设计一个新的设计模式,可以命名为“智能代理模式”,用于智能地代理复杂操作。 #### 代码重构 - **步骤**:识别问题区域、编写测试、重构代码、验证测试。 - **原因**:提高代码质量、增强可维护性。 #### ...