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.

blog.JPG

           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.

//TargetInterface.java
//Define the interface the target class implements

public interface TargetInterface{
     public void print();
}

//TargetImplementation.java
//The class implements the interface

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

//ProxyTarget.java
//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(),
                                                                           obj.getClass().getInterfaces(),
                                                                           new ProxyTarget(obj));
       }

        private Object target;
        private ProxyTarget(Object target)
        {
                this.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;
                 try{
                        //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){
                        System.out.println(e.getMessage());
                 }
                 return result;
          }
}

//TestProxyTarget.java
//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 
                 t.print();
        }
}

The running result of the TestProxyTarget is:

blogcmd.JPG

         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.

xiaoran.jpg

 

Advertisements

6 Comments »

  1. Our company’s headquarters is in Tokyo, so if we want to log in website in China, we have to open the proxy server. Besides this, I know nothing about that and this article is so great!

    Comment by Yuan — October 8, 2007 @ 5:42 am |Reply

  2. Hii..thanks for your info 🙂
    Keep posting bro!
    Unblock MySpace, Youtube, Facebook, Friendster, Hi5, etc. try our High Anonymous Proxy with SSL enable, Ads Remover (Strip out Google Adsense, Adbrite, etc. up to 65 ad hosts), Blowfish URL Encryption, Output Optimizer, Hide browser details and more!!!
    Proxiter.com

    Comment by Proxiter.com — December 5, 2007 @ 12:44 am |Reply

  3. i tried adbrite but my ad earnings are not as good as adsense.:-;

    Comment by Justin Campbell — July 10, 2010 @ 1:47 pm |Reply

  4. Adbrite is the ppc program that is second to adsense, i like adbrite`~`

    Comment by Theo Shaw — August 22, 2010 @ 3:00 pm |Reply

  5. i do not use adbrite because their pay rate is horrendously low compared to say infolinks ‘~”

    Comment by Driving Lights — December 4, 2010 @ 3:29 am |Reply

  6. That i seriously like what you may post here. Particularly informative together with reasonable. A particular problem though. I’m running Opera utilizing Debian together with segments to your active page layout sections would definitely be a bit of wonky. That i realise it’s no popular arrange. Yet it’s something that will keep in mind. That i hope that this will assistance together with continue to keep up the top superior posting.

    Comment by Maybell Lockman — April 18, 2011 @ 8:23 am |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: