Spring AOP offers a wealth of new options in programming as well as designing Java applications. A somewhat more advanced feature is the Hot Swappable Target Source. The concept of a hot swappable target source is linked to the use of proxies instead of concrete object implementations, which is the heart of standard, run-time JDK based AOP. To advise an object with aspects, such as described in my previous post Getting into Spring AOP – Implementing simple business logic on top of Domain Objects using Aspect Oriented Programming, a proxy is created. This proxy intercepts method calls intended for the underlying target object and applies aspects for all specified pointcuts. Usually the wrapped target object still gets called somewhere in the middle of executing all the aspects that were advised. Note that the code using the proxy is not aware of the fact that it is not using a 'normal' implementation of the interface it is programmed against but instead a proxy. It does not matter for the code; only when you ask for the myobject.getClass() will get quite another classname than you would expect. However myobject instanceof interface will still result in true. Suppose we have advised an object implementing the Employee interface. The advise adds some validation logic that is executed just prior to invoking the set-methods on the Employee interface (for example to ensure that Salesmen do not get paid too much and that no employee is called John Doe). Since the code using the object is now in reality using a proxy that itself refers a 'real' EmployeeImpl object (EmployeeImpl is a class that implements the Employee interface), it should theoretically be possible to swap one EmployeeImpl currently use as proxy target with another one. Without the program noticing it, it would all of a sudden deal with a completely new underlying object. We will first show that such a thing is indeed possible – and in a fairly simple manner, and then we will discuss why you might one to do something like that. Read the rest of this entry »