Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java Dynamic Proxy

Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google is your friend:
[ May 28, 2006: Message edited by: Ilja Preuss ]
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic