zoukankan
html css js c++ java
最简单的动态代理实例(spring基于接口代理的AOP原理)
JDK的动态代理是基于接口的
package
com.open.aop;
public
interface
BusinessInterface
{
public
void
processBusiness();
}
目标对象
package
com.open.aop;
public
class
BusinessObject
implements
BusinessInterface
{
public
void
processBusiness()
{
System.out.println(
"
action
"
);
}
}
代理对象
package
com.open.aop;
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Method;
import
java.lang.reflect.Proxy;
public
class
LogHandler
implements
InvocationHandler
{
private
Object delegate;
public
Object binder(Object delegate)
{
this
.delegate
=
delegate;
return
Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(),
this
);
}
public
Object invoke(Object proxy, Method method, Object[] args)
throws
Throwable
{
Object obj
=
null
;
System.out.println(
"
对象方法调用前
"
);
try
{
obj
=
method.invoke(delegate, args);
}
catch
(Exception e)
{
}
System.out.println(
"
对象方法调用后
"
);
return
obj;
}
}
测试类
package
com.open.aop;
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Proxy;
public
class
Test
{
public
static
void
main(String[] args)
{
BusinessInterface bi
=
(BusinessInterface)
new
LogHandler()
.binder(
new
BusinessObject());
bi.processBusiness();
}
}
输出结果
对象方法调用前
action
对象方法调用后
查看全文
相关阅读:
Linux下Maven的安装与使用
Vue1.0用法详解
一个异步访问redis的内存问题
jquery和zepto的异同
我的学习归纳方法(以学习Maven为例)
最显而易见的设计最容易成功
Linux Command Backup
Turn and Stun server · J
Android apk签名详解——AS签名、获取签名信息、系统签名、命令行签名
Leetcode 981. Time Based Key-Value Store(二分查找)
原文地址:https://www.cnblogs.com/shaohz2014/p/3582151.html
最新文章
Android Studio实现Service AIDL
Linux操作系统入门教程
Ubuntu 14.04 使用MyEclipse 10.7 闪退解决
Massively parallel supercomputer
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Weixin' did not find a matching property.
JAVA的不同加密方式及不同的实现方式集合。
开发模式接入
微信小程序及开发工具介绍
MySQL实用第三方客户端工具收集
热门文章
MySQL实用第三方客户端工具收集
MySQL实用第三方客户端工具收集
如何做一名专业的软件测试工程师
如何做一名专业的软件测试工程师
如何做一名专业的软件测试工程师
聊聊 HTTPS 和 SSL/TLS 协议
聊聊 HTTPS 和 SSL/TLS 协议
聊聊 HTTPS 和 SSL/TLS 协议
最实用前端开发框架对比评测
如何用穷举法求解截断切割问题?
Copyright © 2011-2022 走看看