Spring’s AOP implementation

Not all AOP frameworks are created equal. They may differ on how rich of a joinpoint model they offer. Some may allow you to apply advice at the field modification level, while others only expose the joinpoints related to method invocations. They may also differ on how and when they weave the aspects. Whatever the case, the ability to create pointcuts that define the joinpoints at which aspects should
be woven is what makes it an AOP framework. Although there are several implementations of AOP, right now we are concerned with how Spring implements AOP. So let’s take a look at the key points of
Spring’s AOP framework.

Spring advice is written in Java

All of the advice you create within Spring will be written in a standard Java class.
This means you will get the benefit of developing your aspects in the same integrated development environment (IDE) you would use for your normal Java development. What’s more, the pointcuts that define where advice should be applied are typically written in XML in your Spring configuration file. This means both the aspect’s code and configuration syntax will be familiar to Java developers.

Other frameworks out there, specifically AspectJ, require a special syntax to write the aspect and define pointcuts. There are benefits and drawbacks to this approach. By having an AOP-specific language, you get more power and finegrained control, as well as a richer AOP toolset. However, you are required to
learn a new tool and syntax to accomplish this.

Spring’s advises objects at runtime

Spring does not create a proxied object until that proxied bean is needed by the application. If you are using an ApplicationContext, the proxied objects will be created when it loads all of the beans from the BeanFactory. Because Spring creates proxies at runtime, you do not need a special compiler to use Spring’s AOP.

Spring generates proxied classes in two ways. If your target object implements an interface(s) that exposes the required methods, Spring will use the JDK’s java.lang.reflect.Proxy class. This class allows Spring to dynamically generate a new class that implements the necessary interfaces, weave in any advice, and proxy any calls to these interfaces to your target class.

If your target class does not implement an interface, Spring uses the CGLIB1 library to generate a subclass to your target class. When creating this subclass, Spring weaves in advice and delegates calls to the subclass to your target class. When using this type of proxy generation, you need to deploy all of the JAR files in the lib/cglib directory of your Spring distribution with your application. There are two important things to take note of when using this approach:

  • Creating a proxy with interfaces is favored over proxying classes, since this leads to a more loosely coupled application. The ability to proxy classes is provided so that legacy or third-party classes that do not implement interfaces can still be advised. This approach should be taken as the exception, not the rule.
  • Methods marked as final cannot be advised. Remember, Spring generates a subclass to your target class. Any method that needs to be advised is overridden and advice is woven in. This is not possible with final methods.

Note: CGLIB is an open source, high-performance code generation library. You can find more information about CGLIB at http://cglib.sourceforge.net.

Spring implements AOP Alliance interfaces

The AOP Alliance is a joint project between several parties interested in implementing AOP in Java. The AOP Alliance shares the same belief as Spring that can provide cleaner and easier solutions for Java enterprise applications than is currently offered by EJB. Their goal is to standardize Java’s AOP interface to provide interoperability between various Java AOP implementations. This means AOP advice that implements their interfaces (as do some of Spring’s implementations) will be reusable in any other AOP Alliance–compatible framework.


Spring only supports method joinpoints

As mentioned earlier, multiple joinpoint models are available through various AOP implementations. Spring only supports method joinpoints. This is in contrast to some other AOP frameworks, such as AspectJ and JBoss, which provide field joinpoints as well. This prevents you from creating very fine-grained advice, such as intercepting updates to an object’s field.

However, as Spring focuses on providing a framework for implementing J2EE services, method interception should suit most, if not all, of your needs. Plus, Spring’s philosophy is that field interception violates encapsulation. A fundamental object-oriented concept is that objects initiate operations on themselves and other objects through method calls. Having advice fired on field modification as opposed to method invocation arguably violates this concept. Now you have a general idea of what AOP does and how it is supported by Spring. Let’s take a look at how to create the different advice types in Spring.