Core Java

Understanding Dynamic Proxy: Spring AOP Basics

Why AOP :

To Understand AOP(Aspect Oriented Programming), we need to understand Cross Cutting Concerns in software development . In every project , there is some amount of code which gets repeated in multiple classes , across several modules.For example logging which needs to be done for almost all classes and for all module.

This kind of code reduces code’s re usability , maintainability and scalability.For example , if you want to take the class and reuse it somewhere else , where logging is not needed , you have to change this class . Similarly, if validation or logging is changed , we have to change almost each and every class wherever this is used .

A class should focus on it’s core functionality , things like logging ,validation , transaction etc are not part of class functionality . Aspect Oriented Programming is the paradigm to help us removing these cross cutting concerns. It provides us the tools and methodology to separate our cross cutting code from classes .

Proxy Pattern:

We can create a proxy of the object , which will take care of the cross cutting concern code.There are two kind of proxy patterns :

  • Static Proxy :

    Where we create a proxy object for every class. This is not feasible and practical

  • Dynamic Proxy :

    In this , proxies are created dynamically through reflection . This functionality is added from JDK 1.3 . dynamic Proxy form the basic building block of Spring AOP

Here Class Example1 implements interface BasicFunc .


public class Example1 implements Basicfunc{

 public void method1() {
  System.out.println("executing method 1");  


Here is the Interface Basicfunc


public interface Basicfunc{

 public void method1();       


Now if we want to calculate execution time of method1 , we have to write that code in method itself , or we can create a proxy object. For creating proxy object , we create a Invocationhandler like this :


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler{

 private Object target ;

        public MyInvocationHandler(Object target)
 { = target;

 public Object getTarget() {
  return target;

 public void setTarget(Object target) { = target;

 public Object invoke(Object proxy, Method method, Object[] params)
   throws Throwable {
  long a = System.currentTimeMillis();
  Object result =method.invoke(target ,params);
  System.out.println("total time taken  "+
  return result;


In this invocation handler , we call the actual method as well as calculate the time taken.Now , in main class we create the proxy object by using Proxy class.


import java.lang.reflect.Proxy;

public class MainClass {
      public static void main(String[] args) {

       Example1 ex = new Example1();

       Basicfunc proxied =(Basicfunc)Proxy
         ex.getClass().getInterfaces() ,new MyInvocationHandler(ex));

For creating Proxy we pass classloader[mostly the same classloader as origin class],interfaces,and the invocationHandler (pass the original target object in the invocation handler ). The original class must implement a interface ,only those method declared in interface get proxied and then we cast the proxy to the interface type .

If you get Exception like this : java.lang.ClassCastException: $Proxy0 cannot be cast to , it means your target class does not implement the interface.

In CGLib Proxy , there is no necessity of declaring interface. So , this is how we made sure that our Example class write code for only method1 and we kept execution time calculation code out of it. It is a very basic example , but it is the basic of Spring AOP. Other frameworks implementing aop other than Spring AOP are AspectJ and JBOSS AOP

Reference: Understanding Dynamic Proxy: Spring AOP Basics from our JCG partner Abhishek Somani at the Java, J2EE, Server blog.

Abhishek Somani

Abhishek is working as a senior java developer in a product start up .He has worked on various java related enterprise applications and frameworks. He loves to explore new technologies
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Neo Lite
Neo Lite
10 years ago

Hi Abhishek, I am not trying to be harsh here. The title is a little misleading. You tried to explain proxy pattern more that you explained Spring AOP. When you mean AOP basics, the expectation was that you would explain the AOP concepts first, proxy is just a way in which AOP is achieved. Then you had to explain how Spring helps in AOP.

Appreciate your efforts in putting up the article. Keep posting.


10 years ago
Reply to  Neo Lite

agreed ..title is misleading ..will be careful next time ..thnx for your suggestion :)

Back to top button