惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
S
SegmentFault 最新的问题
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
爱范儿
爱范儿
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
DataBreaches.Net
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
D
Docker
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
宝玉的分享
宝玉的分享
腾讯CDC
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
T
The Exploit Database - CXSecurity.com
T
The Blog of Author Tim Ferriss
V
V2EX
S
Securelist
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Y
Y Combinator Blog
P
Proofpoint News Feed
T
Tor Project blog
AWS News Blog
AWS News Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
T
Threat Research - Cisco Blogs
B
Blog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
N
News and Events Feed by Topic
博客园 - 司徒正美
H
Help Net Security
C
Cisco Blogs
C
Check Point Blog
S
Secure Thoughts

博客园 - kongxx

Java & .Net通讯问题 无异间发现的好网站 Message: 因为当前线程不在单线程单元中,故无法实例化 ActiveX 控件 错误处理方法 使用log4net记录日志到MySQL中 C#的MD5实现 Java的MD5实现 PHP实现的Telnet 【引用】 microsoft WINDOWS 系统错误代码 [收藏] PicoContainer学习手册 PicoContainer学习手册 [导入]Digester学习笔记 [导入]使用对Ant编程来实现简单文件的打包 [导入]关闭WinXP自带的Zip功能 [导入]使Tomcat可以下载中文文件 [导入].Net中使用com组件后发生System.ArithmeticException异常的解决办法 [导入]使用简单的Web开发架构 [导入].Net配置文件常用配置说明 [导入]使控件拥有透明背景色 [引用] [导入].Net配置log4net
Java动态代理实现
kongxx · 2005-08-08 · via 博客园 - kongxx

Java动态代理实现

Kongxx

在目前的Java开发包中包含了对动态代理的支持,但是其实现只支持对接口的的实现。

其实现主要通过是java.lang.reflect.Proxy类和java.lang.reflect.InvocationHandler接口。

Proxy类主要用来获取动态代理对象,InvocationHandler接口用来约束调用者实现,如下,HelloWorld接口定义的业务方法,HelloWorldImplHelloWorld接口的实现,HelloWorldHandlerInvocationHandler接口实现。代码如下:

业务接口:

public interface HelloWorld {

       void sayHelloWorld() ;

}

业务接口实现:

public class HelloWorldImpl implements HelloWorld {

       public void sayHelloWorld() {

              System.out.println("Hello World!");            

       }

}

InvocationHandler实现,需要在接口方法调用前后加入一部份处理工作,这里仅仅在方法调用前后向后台输出两句字符串,其代码如下:

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

public class HelloWorldHandler implements InvocationHandler {

       //要代理的原始对象

       private Object objOriginal;

       /**

        * 构造函数。

        * @param obj 要代理的原始对象。

        */

       public HelloWorldHandler(Object obj) {

              this.objOriginal = obj ;

       }

       public Object invoke(Object proxy, Method method, Object[] args)

                     throws Throwable {

              Object result ;

        //方法调用之前

              doBefore();

        //调用原始对象的方法

              result = method.invoke(this.objOriginal ,args);

        //方法调用之后

              doAfter();

              return result ;

       }

       private void doBefore() {

              System.out.println("before method invoke!");

       }

       private void doAfter() {

              System.out.println("after method invoke!");

       }

}

测试代码:

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Proxy;

public class Test {

       public static void main(String[] args) {

              HelloWorld hw = new HelloWorldImpl();

              InvocationHandler handler = new HelloWorldHandler(hw);

              HelloWorld proxy = (HelloWorld) Proxy.newProxyInstance(

                            hw.getClass().getClassLoader(),

                            hw.getClass().getInterfaces(),

                            handler);

              proxy.sayHelloWorld();

       }

}

Ø         首先获取一个业务接口的实现对象;

Ø         获取一个InvocationHandler实现,此处是HelloWorldHandler对象;

Ø         创建动态代理对象;

Ø         通过动态代理对象调用sayHelloWorld()方法,此时会在原始对象HelloWorldImpl. sayHelloWorld()方法前后输出两句字符串。

运行测试类输出如下:

before method invoke!

Hello World!

after method invoke!

此处Test类中的方法调用代码比较多,在我们的实际应用中可以通过配置文件来来简化客户端的调用实现。另外也可以通过动态代理来实现简单的AOP