Five ‘s Weblog

October 8, 2007

Applying Java reflection to Ioc and AOP (2)

Filed under: Programming — by powerdream5 @ 3:30 am
Tags: , , ,

        Today, let’s continue the example of applying java reflection to AOP(Aspect-oriented programming). Because the core of AOP is proxy, I want to make a brief description of it firstly.
        The two important tasks of proxy are interface implementation and delegation. Interface implementation means the proxy class need to implement all the interfaces the target class implements, and delegation means the proxy will catch all the calls to the target class, so that it is easy to add functions before or after the target methods.


           java.lang.reflect.Proxy helps us create a proxy of the target class dynamically, and java.lang.reflect.InvocationHandler interface will allow the proxy class delegate the calls to the target class.
          The following example shows how to use proxy to realize the function of logging. By extracting the function of logging from the method, we can concentrate our method in business logic.

//Define the interface the target class implements

public interface TargetInterface{
     public void print();

//The class implements the interface

public class TargetImplementation implements TargetInterface{
    public void print(){
          System.out.println(“print method”);

//Create the proxy class and delegate the calls to the target class

import java.lang.reflect.*;
public class ProxyTarget implements InvocationHandler{
      public static Object createProxy(Object obj){
            //creat the proxy class according the instance of the target class                 
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(),
                                                                           new ProxyTarget(obj));

        private Object target;
        private ProxyTarget(Object target)
       = target;
        //Implements the method of InvocationHandler interface
        //This method will catch all the calls to the target class
        //and redispatch them       

        public Object invoke(Object proxy, Method method, Object[] args)
                 Object result = null;
                        //add function before redispatching
                        System.out.println(“Before “+method.getName());
                        //redispatch the call
                        result = method.invoke(target,args);
                        //add function after redispatching
                        System.out.println(“After “+method.getName());
                 }catch(Exception e){
                 return result;

//the test class

public class TestProxyTarget{
        public static void main(String[] args){
                 TargetInterface t = (TargetInterface)ProxyTarget.createProxy(
                                                                                   new TargetImplementation());
                 //this will call the invoke method of the ProxyTarget class automatically and transparently 

The running result of the TestProxyTarget is:


         After reading the examples, I hope you can have some basic ideas of Ioc and Aop, and figure out the relationship between java reflection and Ioc and AOP. Of course, if you want to learn more, please search more materials about them.



Create a free website or blog at