Five ‘s Weblog

October 7, 2007

Applying Java reflection to Ioc and AOP (1)

Filed under: Programming — by powerdream5 @ 1:10 am
Tags: , ,

reflection

         JDK1.5 has introduced many useful features, including annotation and reflection. And now, IOC(Inversion of Control) and AOP(Aspect-oriented programming) would be the most popular words in the J2EE field. Today, I am not going to explain what is IOC and what is AOP. I just want to show two examples to present the technology of java reflection plays an important role in IOC and AOP.

        The first example would be a little bit different from the the concept of IOC, but the common thing is that both load the calss dynamically. In this example, I use properties file as the configuration file, but in a real application, we mostly use xml files.

//InjectionInterface.java 
//Firstly, we define a interface

public interface InjectionInterface{
       public void print();
}

//InjectionClass_1.java
//implements the InjectionInterface
public class InjectionClass_1 implements InjectionInterface{
       public void print(){
               //print out the name of the class the object belongs to
               System.out.println(“the print method of class InjectionClass_1”);
       }
}

//InjectionClass_2java
//implements the InjectionInterface

public class InjectionClass_2 implements InjectionInterface{
      public void print(){
              //print out the name of the class the object belongs to
              System.out.println(“the print method of class InjectionClass_2”);
     }
}

//Ioc.java-the testing class 
import java.util.Properties;
import java.io.*;

public class Ioc{
      private InjectionInterface injectionClass;

      //using setter injection
     public void setInjectionClass(InjectionInterface injectionClass)
    {
            this.injectionClass = injectionClass;
    } 

    public void print()
   {
            injectionClass.print();
    }

    public static void main(String[] args)
   {
            try{
                  Properties props = new Properties();
                  props.load(new FileInputStream(“injection.properties”));
                  String classname = props.getProperty(“iocclass”);
                 //dynamically load the class according to the class name
                  Class cls = Class.forName(classname);
                  Ioc ioc = new Ioc();
                 //get an instance of the loaded class
                  ioc.setInjectionClass((InjectionInterface)cls.newInstance());
                  ioc.print();
            }catch(Exception e){
                    e.printStackTrace();
            }
        }
}

//injection.properties – the properties file
iocclass=InjectionClass_1

The running result of the program is: the print method of class InjectionClass_1

Now, you can change the properties file: iocclass=InjectionClass_2. you don’t need to compile the java files again, and just run the class directly. you can see the result would be different.

ioc.JPG

        After reading the example, I hope you have a better understanding of IOC. And tomorrow, I will show the example about using the technology of reflection in AOP.

Advertisements

3 Comments »

  1. Great. Show something about others

    Comment by chen — October 7, 2007 @ 3:54 am |Reply

  2. It is so professional. Fortunately, I can understand. However, I only can read it but can’t write it as you did.

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

  3. hi,
    I’m new in JMock, i’ve started with your example, it seems interesting to start with, but, I couldn’t execute it, So the class path is set. Is there other things to add to this program???

    Comment by anis — July 17, 2008 @ 9:51 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

Blog at WordPress.com.

%d bloggers like this: