Spring 2.0 AOP odds and ends - No Fluff Just Stuff

Spring 2.0 AOP odds and ends

Posted by: Craig Walls on February 25, 2006

I've been tinkering around with Spring 2.0 lately and have found the new configuration elements quite interesting.

In case you've not heard the news, Spring 2.0 comes with a handful of new XML configuration elements that simplify things that would otherwise require complex <bean> definitions. One of the schemas that define some new elements is spring-aop.xsd which, among other things, defines a short-and-sweet way of declaring aspects using simple POJOs.

For example, consider the following snippet from a Spring configuration:

  <aop:config>
    <aop:aspect id="judgeAspect" ref="simon">
       <aop:advice
          kind="after"
          method="commentOnPerformance"
          pointcut="execution(* *..Performer.perform(..))" />
    </aop:aspect>
  </aop:config>  

This XML snippet defines an aspect. In short, this aspect says that after the execution of the Performer.perform() method, the commentOnPerformance() method should be called on the bean whose id is "simon".

The thing to key on for the moment is the "kind" attribute. This attribute tells Spring what kind of advice is being applied; that is, whether it is "before", "after", "after-returning", "after-throwing", or "around" advice.

This isn't exactly new news. In fact, it was first demonstrated in Florida at The Spring Experience in December. But here's what's new...

While playing around with the latest nightly build of Spring 2.0, I couldn't help but notice that Adrian Colyer has added some new configuration elements for defining advice:

  • <aop:before>
  • <aop:after>
  • <aop:after-returning>
  • <aop:after-throwing>
  • <aop:around>

According to the comments left in spring-aop.xsd, these elements are now the preferred way to define advice. Instead of using the "kind" attribute to determine what kind of advice is applied, the XML element itself will define the kind of advice. For example, the aforementioned aspect should be defined this way:

  <aop:config>
    <aop:aspect id="judgeAspect" ref="simon">
       <aop:after
          method="commentOnPerformance"
          pointcut="execution(* *..Performer.perform(..))" />
    </aop:aspect>
  </aop:config>  

These new advice configuration elements aren't much simpler (it couldn't get much simpler than they already are), but they are a bit more clear in their purpose (in that you don't have to read the "kind" attribute to know what kind of advice is being applied).

It's little changes like this that make me excited to see that Spring 2.0 is being released soon. This new stuff is much simpler than the pre-2.0 way of defining an aspect using ProxyFactoryBean.

Craig Walls

About Craig Walls

Craig Walls is a Principal Engineer, Java Champion, Alexa Champion, and the author of Spring AI in Action, Spring in Action, and Build Talking Apps. He's a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring. When he's not slinging code, Craig is planning his next trip to Disney World or Disneyland and spending as much time as he can with his wife, two daughters, 1 bird and 2 dogs.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »