BT

InfoQ Homepage News InfoQ Article: Simplifying Enterprise Apps with Spring 2 and AspectJ

InfoQ Article: Simplifying Enterprise Apps with Spring 2 and AspectJ

Bookmarks
Spring has long provided a proxy-based service to apply Aspects to Spring-managed beans. However, when it comes to requirements that impact multiple points in your domain model, Spring AOP is of much less assistance, but AspectJ is a natural fit.  AspectJ aspects don't need any special proxy creation, and can happily advise objects created at runtime either in your application code or by frameworks you may be using. AspectJ is also a very good solution when you want to modularise behaviour that cuts across all the different layers of your application, or that is in any way performance sensitive.

Adrian Colyer, AspectJ lead and Chief Scientist at Interface21 has contributed an excellent article which shows how to use Spring 2's new AspectJ integration features followed by a roadmap for the adoption of Aspect Oriented Programming on an enterprise project, with lots of specific examples of how and where to apply Aspects.

Adoption of AOP can proceed in a number of phases: start out by taking advantage of aspects that Spring supplies out-of-the-box, and then you can add your own @AspectJ aspects using Spring AOP in the web, service, and data-access layers. AspectJ itself can be used to improve development productivity without introducing any runtime dependency on AspectJ. Going further, infrastructural requirements that cut across multiple layers of your application can be simply implemented using AspectJ aspects. Finally, you can use aspects to simplify the implementation of your domain model itself.
Read Simplifying Enterprise Apps with Spring 2 and AspectJ.  See also infoq.com/spring, infoq.com/aop

Adrian is also speaking at The Spring Experience, along with the rest of the spring experience.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Excellent article

    by Jonas Bonér /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I enjoyed reading it.
    Thanks Adrian.

  • infoq.com/spring

    by Geoffrey Wiseman /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    What's "infoq.com/spring"? A topic? Why isn't it listed in the topics above?

  • Re: infoq.com/spring

    by Floyd Marinescu /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    What's "infoq.com/spring"? A topic? Why isn't it listed in the topics above?


    If you look beneath the body of the article, you'll see "Tags Spring, AspectJ". You can click on those to see any all content on InfoQ about Spring or AspectJ. If you look at the top of the article you can see "topic - AOP", which functions the same. I added a link manually to the news item linking to this article so that people would notice the functionality.

  • Great article!

    by Michael Klishin /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Very nice article on AOP. Probably the most clean explanation of AOP terminology and principles that I've ever read. Keep on a great job, i21 & InfoQ!

  • Printing article on Firefox is not correct

    by Peter Bona /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    When clicking on the print image right above, Firefox (1.5.0.6) is leaving some text out on the right, you cannot see the whole text. IE works fine. Could you pls check if there is something you could do about it? Thanks!

    Thanks for the article, it is really nice!

  • Re: Printing article on Firefox is not correct

    by Floyd Marinescu /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi Peter, what page/paragraph do you notice the problem? I have the same version and it seems to print fine.

  • Re: Printing article on Firefox is not correct

    by Peter Bona /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Like this page. I mean www.infoq.com/articles/Simplifying-Enterprise-Apps.

    I was playing with the screen resolution and settings in Print Preview, but none of the attempts worked. Anyway, I managed to print it in IE and that is fine for now.
    Thanks.

  • afterThrow()

    by Cedric Beust /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi Adrian,

    Quick question: in the section about throwing, it's not clear that your afterThrowing() pointcut *replaces* the initial exception. I'm assuming that's what it does, right? It wouldn't make much sense to throw the original exception and then a DAOException, but you might want to clarify how this replacement actually works.

    --
    Cedric

  • Re: afterThrow()

    by Adrian Colyer /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi Cedric,
    Your interpretion is correct. If after throwing advice itself throws an exception, then the exception thrown by the advice will be seen by the client. If the after throwing advice completes normally, then the original exception will propagate to the client.

    The consequence of this is that you can use after throwing for exception *translation*, or for logging or taking other additional actions, but you can't use it to *handle* an exception condition. For that you have to use around advice, with a pattern that looks like this:


    Object around() : some_pc() {
    try {
    return proceed();
    }
    catch(ExceptionToBeHandled ex) {
    // handle condition here
    return exceptionalConditionReturnValue;
    }
    }


    (or the equivalent in @Aspect or Spring XML styles).

    Regards, Adrian.

  • Sources

    by William Louth /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi Adrian,

    Are you planning on releasing the sources. It would be great to show how one you easily implement the profiling strategy mapping to our Tracer API (www.jinspired.com/products/jxinsight/api/com/ji...) and have it easily integrated within our management console while adding additional JMV resource metrics (cpu, thread waiting, thread blocking, gc, object allocs).

    We previously published a performance insight article based on our Spring 1.x trace extension.
    www.jinspired.com/products/jxinsight/springtrac...

    Regards,

    William Louth
    JXInsight Product Architect
    JInspired

    "Java EE tuning, testing, tracing, and monitoring with JXInsight"
    www.jinspired.com

  • controller aspectj question

    by Weng Guerra /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Hi,
    Thanks for the good article. Though I have a quick question.
    Just want to ask how would I be able to forward a user to an error page after using pointcut on that method. Say I have method in com.my.controllers.AnswerQuestion

    processQuestion(blah, blah){
    //do something
    return “success”;
    }

    then I have this aop config

    <aop:config>
    <aop:aspect ref="exceptionHandler">
    <aop:after-throwing
    throwing="theException"
    pointcut="com.my.controllers.AnswerQuestionprocessQuestion()"
    method="theExceptionHandlerMethod"/>
    </aop:aspect>
    </aop:config>

    The main question is how would you redirect the user not to “success” but to “errorpage”. Ofcourse I dont want to do big change on processQuestion() method, the aim is to let the AOP manage it.

    Thanks!

  • Re: controller aspectj question

    by Sebastian Jancke /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Weng Guerra,

    Try something like:

    Thats my class:
    public class Bar {

    public String foo(int x) {
    return "success";
    }
    }

    And with this Aspect, i replace the outcome of foo(x):

    public aspect ReplaceAspect {
    pointcut yourMethodPointcut(Bar bar, int x):
    target(bar) && args(x) &&
    call(public String Bar.foo(int));

    String around(Bar bar, int x) : yourMethodPointcut(bar, x) {
    proceed(bar, x);
    return "errorpage";
    }
    }

    -Sebastian Jancke

  • Re: controller aspectj question

    by Sebastian Jancke /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    (Sorry, I forgot the html-pre...)

    Weng Guerra,

    Try something like:


    public class Bar {
    public String foo(int x) {
    return "success";
    }
    }

    And with this Aspect, i replace the outcome of foo(x):

    public aspect ReplaceAspect {
    pointcut yourMethodPointcut(Bar bar, int x):
    target(bar) && args(x) &&
    call(public String Bar.foo(int));

    String around(Bar bar, int x) : yourMethodPointcut(bar, x) {
    proceed(bar, x);
    return "errorpage";
    }
    }


    -Sebastian Jancke

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.