• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

uses of aop in spring

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked this once before, but I'm curious if the answers changed. And I never asked in the context of Spring.

What are some common uses of AOP in a Spring application? I can think of logging and validation. What others have people used?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't remember using it explicitly (by defining my own aspects), but I used it implicitly by using Spring's transaction management.
 
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AOP is commonly described as a way to implement "cross-cutting concerns"...which means a way to define, in one place, functionality that's needed in multiple places throughout an application's code.

That's true, but aspects are also useful even if they'll only be applied in a single place. The point is that aspects are a way of separating some functionality from the thing that needs it....regardless of how many things need it. That is, you can use aspects to factor functionality out of methods when that functionality is not cohesive within the scope of the method it should be applied to.

For example: Auditing is has nothing to do with transferring money from one account to another. But it needs to happen when that money is transferred. Do you put the auditing functionality directly in the transfer method? Or would you extract it into an aspect?

That said, Spring itself uses aspects for things like transactions, security, caching, and those kinds of things.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those are the biggies--transactions, security, logging/auditing.

I actually have a few books on AOP design written around the time it was a buzzword. AOP can also be used for various types of business logic, DI outside of a DI container (like entity objects, which aren't instantiated by Spring, say), managing constraints used across an application (code-based business rules, for example), enhancing a system without modifying existing code, etc.

To be honest I've never been sure that the Java view of AOP is the best way to do some of the non-obvious stuff, but since Java doesn't have a lot of useful behavior (mixins, trivial ways of adding/modifying behavior) it's sometimes the only realistic, quasi-supported way of things.
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was just reminded of another use of AOP...albeit, AspectJ's form of AOP, not Spring AOP...

Inter-type declarations (ITDs) are a way of tacking new functionality onto an existing class. The closest analogy I can think of is C#'s partial classes. Unfortunately Java doesn't have that ability...but with AspectJ you can certainly separate concerns within a given class by extracting certain bits into an ITD.

For example, a typical entity class may have a handful of concerns: Accessor methods for its properties, toString()/equals()/hashcode() methods, and perhaps the ability to inject it with dependencies so that it can have some richer functionality than just being a data-holder. Each of those concerns could be kept in a separate ITD and the main entity class could just declare the properties (without setters and getters).

That is, in fact, one of the ways that Spring Roo uses ITDs. When you create an entity in Roo, the entity class is fairly empty. The real functionality it kept in a handful of ITDs (which are managed by Roo). Roo also uses ITDs to handle scaffolding in Spring MVC controllers...a basic Roo-generated controller class is empty...but the scaffolding is kept in an ITD.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, Roo uses quite a bit of that, doesn't it? As you said, that does require AspectJ itself, since Spring only supports a subset of AOP. (Although maybe there's more in Spring 3? I haven't checked yet.)
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah...Spring AOP covers most of the AOP needs you'll ever have. But AspectJ is certainly more powerful in some areas.

The cool thing about Roo's ITDs is that they are compile-time aspects. By the time the app is wrapped up in a WAR file, AspectJ's job is done and isn't necessary at runtime.
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Craig Walls wrote:Yeah...Spring AOP covers most of the AOP needs you'll ever have. But AspectJ is certainly more powerful in some areas.

The cool thing about Roo's ITDs is that they are compile-time aspects. By the time the app is wrapped up in a WAR file, AspectJ's job is done and isn't necessary at runtime.



Even JAXB XJC does generate the code from XSD schema but ITDs in Roo make the difference. Where as XJC can generate simple getters/setters.

Craig Walls wrote:For example: Auditing is has nothing to do with transferring money from one account to another. But it needs to happen when that money is transferred. Do you put the auditing functionality directly in the transfer method? Or would you extract it into an aspect?



Can we create such a Auditing Aspect in Spring using Roo?
 
Craig Walls
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Vupputuri wrote:Can we create such a Auditing Aspect in Spring using Roo?



You can certainly create an auditing aspect in Spring. Using Roo? There's nothing in Roo (yet) that adds such a thing automatically. So, you can build you app in Roo, but then to get the auditing aspect, you'll have to add it yourself.
 
Anil Vupputuri
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Craig Walls wrote:

Anil Vupputuri wrote:Can we create such a Auditing Aspect in Spring using Roo?



You can certainly create an auditing aspect in Spring. Using Roo? There's nothing in Roo (yet) that adds such a thing automatically. So, you can build you app in Roo, but then to get the auditing aspect, you'll have to add it yourself.



Thanks for your quick reply. Are there any such examples (custom Aspects such as Auditing) given in SiA3 under AOP section?
 
reply
    Bookmark Topic Watch Topic
  • New Topic