File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java Dynamic Proxy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Dynamic Proxy" Watch "Java Dynamic Proxy" New topic

Java Dynamic Proxy

Alan Hermin
Ranch Hand

Joined: Feb 16, 2006
Posts: 290
Hi rancher...
can any one tell me the details of the java Dynamic Proxy ,i reed the chapter04 of the Manning Java Reflection,please tell me about this, and java.lang.reflect.Proxy
Ravindra Rawat
Ranch Hand

Joined: Dec 09, 2004
Posts: 34
You can think Proxy to be a wrapper over a java object which gives you a chance to intercept method invocations on the object. Your application would create a "proxy" for your object and hand it over to your clients, any method invocations on proxy would be handled by invocation handler specified for the proxy.

Ability to intercept ,method calls gives you ability to add common functionality to your objects e.g logging, security checks etc.

- Ravindra
Nikhil Goel
Ranch Hand

Joined: Jul 11, 2003
Posts: 32
Java Dynamic Proxies is the java answer for AOP programming . AOP is going to be another big thing like OOPS.

Java dynamic proxies works on the concept of interception . What it means is that at run time some handler would intercept the call to called object and invoke the methods on the object . This means you are detached from actual implementation . The same things happen in EJB's . Infact whole JBOSS is written using dynamic proxies .
Slightly confusing !!.

Here it goes how ..

Lets say you have an interface and some implementing objects for that interface. Now before actual call at run time to the methods you want to intercept the calls to the methods and provide some functionality like logging etc .

So you create a Proxy class that does that for you .

Here is a simple example ...

public interface Nikhil {
public String test();
public String test1();


Implementing Object
public class MyNikhil implements Nikhil {
public String test1(){
System.out.println("Hi Nikhil");
return "nikhil";
public String test(){
System.out.println("Hi Nikhil1");
return "nikhil1";

Proxy & Invocation Handler class

import java.lang.ref.*;
import java.lang.reflect.InvocationHandler;

public class Test1 {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
MySalary mgrSalary = new ManagerSalary();
Nikhil myNikhil = new MyNikhil();
Object sal = (Object)java.lang.reflect.Proxy.newProxyInstance(
new Class[] {Salary.class,Nikhil.class},
new Test1().new SalaryHandler( myNikhil ) ) ;
Nikhil test= (Nikhil)sal;


private class SalaryHandler implements InvocationHandler{
Object object;
public SalaryHandler(Object object1){
object = object1;

/** a generic, reflection-based secure invocation */
public Object invoke(Object target,
java.lang.reflect.Method method, Object[] arguments)
throws Throwable {
// call framework and then reflect the app-logic
System.out.println("Method Name is "+method.getName());
return method.invoke(object,arguments);
} catch(java.lang.reflect.InvocationTargetException e) {
// reconvert nested application exceptions
throw e.getTargetException();

}//end of SalaryHandler class

The output of main method will be different .Here in this example Object sal is the proxy for the Nikhil Interface ..

Hope it helps .
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Google is your friend:
[ May 28, 2006: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Java Dynamic Proxy
It's not a secret anymore!