Lets see the magic of Spring, although at a very base level but would give you a fair idea about Spring AOP/IOC and particularly the ‘Proxy’. I ll be using plain java to show how the Proxy pattern works.

Lets take up a simple Interface depicting our business requirement

public interface IBusiness {
    public String getData();
}

Lets take its implementation class

public class BusinessImpl implements IBusiness {
    String data;
    public BusinessImpl() {
        System.out.println("BusinessImpl: Constructor invoked");
        this.data= "spring without spring";
    }

    public String getData() {
        System.out.println("BusinessImpl: getVersion invoked");
        return version;
    }
}

Now, since we don’t want anyone to know what kind of BusinessImpl we have, we need a factory, that simply returns to our users the IBusiness interface. Beneath, It may be Hibernate/JDBC/IBAtis or some abstract logic of our own (considering our business is considered with accessing data).

public class Factory {
    private static Factory factory = new Factory();
    public static Factory instance() {
        return factory;
    }

    public IBusiness getBusiness() {
        IBusiness business = new BusinessImpl();
        return business;
    }
}

And what the users of our Business would do is Factory.instance().getBusiness() to get a handle to our BusinessImpl, which actually they dont know. Untill now it was just abstarction I was telling you about. Now lets look into actually creating a proxy, lets say a LoggerProxy.
Any Proxy in java must implement java.lang.reflect.InvocationHandler Interface and override its public Object invoke(Object proxy, Method method, Object[] args) throws Throwable; method

public class LoggerProxy implements InvocationHandler {
    IBusiness target;
    public LoggerProxy(IBusiness target) {
        this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
              throws Throwable {
        String result = null;
        System.out.println("Log start");
        System.out.println("Logging Proxy .." + proxy.getClass().
              getName());
        result = (String) method.invoke(target, args);
        System.out.println("Log end");
        return result;
    }
}

So, now we have a Proxy, that thats gonna do some logging, invoke our actual business class and again do some logging at exit. However what is missing still is plugging it all together in our factory’s getBusiness() method. So lets change that method a bit

    public IBusiness getBusiness() {
        Class[] interfaces = new Class[] { IBusiness.class };
        IBusiness bus = new BusinessImpl();
        IBusiness business = (IBusiness) Proxy.newProxyInstance(
             IBusiness.class.getClassLoader(),
             interfaces,
             new LoggerProxy(bus));
        return business;
    }

Confused??? OK! What I have done is instantiated a Proxy class for our LoggerProxy using Proxy.newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h) method. Also now Proxy has a handle to our actual business class when we did new LoggerProxy(bus)

Now lets test all that.

public class Springing {
    public static void main(String[] args) {
        IBusiness business = Factory.instance().getBusiness();
        System.out.println("GET --->" + business.getData());
        System.out.println("My business object is a proxy.."
             + business.getClass().getName());
    }
}

Now what you get in output would make it more clear…I wont write it here, instead leave it as an exercise for you to find that fire any question you have for me…Hope you find it interesting as have I. cheers…bye.



No Responses Yet to “Springing – Dynamic Proxy with pure Java”  

  1. No Comments Yet

Leave a Reply