aspose file tools*
The moose likes Spring and the fly likes uses of aop in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "uses of aop in spring" Watch "uses of aop in spring" New topic
Author

uses of aop in spring

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I don't remember using it explicitly (by defining my own aspects), but I used it implicitly by using Spring's transaction management.


[My Blog]
All roads lead to JavaRanch
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 335
    
    5
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.


Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Ranch Hand

Joined: Sep 19, 2003
Posts: 335
    
    5
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Ranch Hand

Joined: Sep 19, 2003
Posts: 335
    
    5
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.
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
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?


SCJP 1.5, SCEA, ICED (287,484,486)
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 335
    
    5
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

Joined: Oct 31, 2000
Posts: 527
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uses of aop in spring