Rick has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

An Introduction to Spring AOP

04.07.2009
| 79028 views |
  • submit to reddit

To see a full description of the AspectJ pointcut language see pointcuts. Note that the bean name pointcut can take you far.

You apply this pointcut and advice around the atm methods using the aop:around tag by referring to the atmLayer. You defined with the aop:pointcut. Then when the atm is looked up with AtmMain (listed below), instead of getting the original atm, you get an AOP proxy, which is like a dynamic decorator.

Figure 3 shows graphically how the objects and advice are found and applied.

Let's complete our example by reviewing the AtmMain main method that looks up the atm out of the Spring applicatoinContext.

Let's review AtmMain as follows:

package com.arcmind.springquickstart.atm;

import java.math.BigDecimal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.arcmind.springquickstart.security.SecurityService;
import com.arcmind.springquickstart.security.SecurityToken;

public class AtmMain {
       
        public static void simulateLogin() {
                SecurityService.placeSecurityToken(new SecurityToken(true, "Rick Hightower"));
        }

        public static void simulateNoAccess() {
                SecurityService.placeSecurityToken(new SecurityToken(false, "Tricky Lowtower"));
        }      
       
        public static void main(String[] args) throws Exception {
               
                simulateLogin();

                ApplicationContext appContext = new ClassPathXmlApplicationContext(
                                                                                        "classpath:./spring/applicationContext.xml");

                AutomatedTellerMachine atm = (AutomatedTellerMachine) appContext
                                .getBean("atm");

                atm.withdraw(new BigDecimal("10.00"));

                atm.deposit(new BigDecimal("100.00"));
               
                System.out.println("done");

        }

}

Before we added AOP support when we looked up the atm, we looked up the object directly as shown in figure 1. Now that we applied AOP, when we look up the object, we get what is in figure 2. When we look up the atm in the application context, we get the AOP proxy that applies the decoration (advice, method interceptor) to the atm target by wrapping the target and delegating to it after it invokes the series of method interceptors.

Conclusion

AOP is neither a cureall or voodoo magic but a powerful tool that needs to be in your bag of tricks. The Spring framework has brought AOP to the main stream masses, and Spring 2.5 has simplified using AOP.

You can use Spring AOP to apply services (called cross-cutting concerns) to objects. AOP need not seem like a foreign concept, as it is merely a more flexible version of the decorator design pattern. With AOP you can add additional behavior to an existing class without writing a lot of wrapper code. This can be a real time saver when you have a use case where you need to apply a crosscutting concern to a slew of classes.

About the Author: 

Rick Hightower (Rick's bio) serves as chief technology officer for ArcMind Inc., a consulting and training firm specializing in Spring, JPA/Hibernate, and JSF. Rick enjoys writing and programming.

 

Published at DZone with permission of its author, Rick Hightower.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Michal Rembiszewski replied on Thu, 2009/04/16 - 3:12pm

I understand you wanted to give a simple example so the AOP concept is easier to grasp. As a side comment I often find myself trying to decide between using an aspect or a static decorator. While AOP seems more versatile at a first glance, it makes your code much more annoying to debug, both interactively and by making stacktraces look ugly. Therefore I usually use it only if the new functionality is required in more than one class (otherwise it isn't really a cross-cutting concern). In case presented in the example I would go with old-style static decorator which is easy enough to configure using spring.

Walter Bogaardt replied on Tue, 2009/05/26 - 11:17am

Simple to understand article. There is always a decision that has to be made in choosing AOP over other designs in your applications. Architecture can be pragmatic or dogmatic as I've seen so much of as technologies evolve and new terms are slapped. I found it useful for creating container independent implementation of persistable jmx beans, transactional support is another area. Yet in all cases it sometimes makes debugging problems a pain, but once you have a solid aspect usually life is good.

Jeroen Rosenberg replied on Fri, 2010/05/07 - 3:16am

Nice article! Recently I've uploaded a presentation on Spring AOP . Might be interesting for you.

Krishnendu Sardar replied on Wed, 2012/09/05 - 5:02am

Hi Can you please provide link to download the whole project?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.