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.




  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!!!

    Comment by — 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

  7. […] Applying Java reflection to Ioc and AOP (2) […]

    Pingback by Java tutorials on Reflection API – Oracle – Defectracker — June 28, 2019 @ 12:31 pm |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: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

Create a free website or blog at

%d bloggers like this: