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

推荐订阅源

Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
博客园_首页
B
Blog
雷峰网
雷峰网
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
D
Docker
博客园 - 司徒正美
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
U
Unit 42
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
腾讯CDC
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
Jina AI
Jina AI
WordPress大学
WordPress大学
D
DataBreaches.Net
V
V2EX
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
P
Privacy International News Feed

博客园 - 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