InfoQ

News

Article: What's New in Spring 2.5: Part 1: Annotation-Based Configuration

Posted by Geoffrey Wiseman on Nov 19, 2007

Community
Java
Topics
Announcements
Tags
Spring

November 19th 2007 was a big day for the Spring Framework.  Spring 2.5 was released, Interface21 has become SpringSource and InfoQ has published the first article in a series of articles by Mark Fisher of SpringSource on the new features:  What's New in Spring 2.5: Part 1: Annotation-Based Configuration.

Describing the series of articles, Mark Fisher wrote:

The newly released Spring 2.5 continues this trend by offering further simplifications and powerful new features especially for those who are using Java 5 or greater. These features include annotation-driven dependency injection, auto-detection of Spring components on the classpath using annotations rather than XML for metadata, annotation support for lifecycle methods, a new web controller model for mapping requests to annotated methods, support for Junit 4 in the test framework, new additions to the Spring XML namespaces, and more.

This article is the first of a three-part series exploring these new features. The current article will focus on simplified configuration and new annotation-based functionality in the core of the Spring application context. The second article will cover new features available in the web-tier, and the final article will highlight additional features available for integration and testing.

If you're currently using the Spring Framework, this is a good opportunity to read about the new features and decide if there's a compelling reason to upgrade.  If you're not using the Spring Framework, but considering it, this is a good opportunity to learn about some of the new features that may make your decision easier, or harder.  Either way, the Spring Framework is a big part of how many people assemble their Java enterprise applications and Mark Fisher will help guide you through the new features in this article and the two parts yet to come.

23 comments

Watch Thread Reply

Missing/hidden XML by Matt Raible Posted Nov 19, 2007 9:51 PM
Re: Missing/hidden XML by Mark Fisher Posted Nov 19, 2007 10:32 PM
Re: Missing/hidden XML by Diana Plesa Posted Nov 20, 2007 3:05 AM
Re: Missing/hidden XML by mohan raj Posted Nov 29, 2007 5:11 AM
Good Stuff by Ray Krueger Posted Nov 20, 2007 8:01 AM
Re: Good Stuff by Mark Fisher Posted Nov 20, 2007 8:35 AM
Re: Good Stuff by Darryl Pentz Posted Mar 4, 2008 2:53 PM
Re: Good Stuff by Archie Cobbs Posted May 12, 2008 9:40 AM
Grate Guice alternative by Tom Nichols Posted Nov 20, 2007 8:11 AM
Annotations vs. XML by Geoffrey Wiseman Posted Nov 20, 2007 8:47 AM
Re: Annotations vs. XML by Rod Johnson Posted Nov 20, 2007 4:47 PM
Re: Annotations vs. XML by Jörg Gottschling Posted Nov 23, 2007 1:30 AM
Re: Annotations vs. XML by Rod Johnson Posted Nov 26, 2007 2:34 PM
lament the passing of design and OO programming by James Richardson Posted Nov 27, 2007 6:05 PM
Great Job by Khaled Habiburahman Posted Nov 28, 2007 2:39 AM
Re: Great Job by mohan raj Posted Nov 29, 2007 5:12 AM
the 2nd part and 3rd part by Nantian Lotus Posted Feb 5, 2008 8:43 AM
Re: the 2nd part and 3rd part by Lou Sacco Posted Mar 27, 2008 2:06 PM
2nd and 3rd parts by Gregor Morrison Posted Jun 3, 2008 4:58 AM
Re: 2nd and 3rd parts by Mohammad Naqvi Posted Aug 21, 2008 1:38 PM
the last part by john wang Posted Sep 8, 2008 3:38 PM
Re: the last part by Lukasz Budnik Posted Nov 19, 2008 6:00 AM
Re: 2nd and 3rd parts by Johan Pelgrim Posted Dec 10, 2008 6:03 AM
  1. Back to top

    Missing/hidden XML

    Nov 19, 2007 9:51 PM by Matt Raible

    There seems to be a fair amount of missing or hidden XML in this article. If you look at the HTML source, you'll see where some XML has not been properly escaped.

  2. Back to top

    Re: Missing/hidden XML

    Nov 19, 2007 10:32 PM by Mark Fisher

    Matt,

    You are right. Someone must have edited this recently, because the XML was showing up properly before. I just sent a mail to the editor.

    Thanks,
    Mark

  3. Back to top

    Re: Missing/hidden XML

    Nov 20, 2007 3:05 AM by Diana Plesa

    Hi Matt, Mark

    The XML has been fixed now.

    Best
    Diana

  4. Back to top

    Good Stuff

    Nov 20, 2007 8:01 AM by Ray Krueger

    I am really looking forward to using these new features, great job guys.

    By the way Mark, your sudden and unexplained use of the p: namespace might freak people out. In your explanation of the lifecycle annotations you declare the datasource using the p: namespace trick from blog.interface21.com/main/2006/11/25/xml-syntax... Unfortunately, many people probably aren't aware of this feature and it isn't actually mentioned in the article.

  5. Back to top

    Grate Guice alternative

    Nov 20, 2007 8:11 AM by Tom Nichols

    This is great -- I always liked the annotation-driven style of Guice but didn't want to abandon Spring just for that. Hooray for less XML!

  6. Back to top

    Re: Good Stuff

    Nov 20, 2007 8:35 AM by Mark Fisher

    Ray,

    Thanks for pointing out that blog for the 'p' namespace. Hopefully that will serve as a 'footnote' now for anyone who may be confused by its usage. Those examples are taken directly from the PetClinic application by the way.

    Thanks,
    Mark

  7. Back to top

    Annotations vs. XML

    Nov 20, 2007 8:47 AM by Geoffrey Wiseman

    I have to admit, I'm content to use XML for wiring, myself -- although perhaps when I try the annotations, I'll discover more advantages than I expect. I seem to be in the minority here.

    That said, I was more impressed by the features here than I expected to be, so next time I start some Spring config., I might give this a try.

    Sorry about the XML; that might have been my fault, I changed some metadata after the initial publishing, and there are some quirks in the publishing process that Diana's better at handling. ;)

    I'm looking forward to the next two parts of the article.

  8. Back to top

    Re: Annotations vs. XML

    Nov 20, 2007 4:47 PM by Rod Johnson

    Geoffrey

    I have to admit, I'm content to use XML for wiring, myself -- although perhaps when I try the annotations, I'll discover more advantages than I expect. I seem to be in the minority here.

    The goal of Spring is to be the ultimate component model. That component model can be configured in different ways. There is no perfect one size fits all approach to configuration. Different contributions are merged together by the container. Certainly, annotations have an important place. However, my experience in practice (long predating my creation of Spring) has been that you need to externalize significant parts of your configuration from Java code.

    The annotation support in Spring 2.5 is very slick and definitely makes Spring a better product. You can mix and match annotation-driven and XML (and other) configuration so that you can use the appropriate solution for each problem. I'm proud that each version of Spring has made applications easier to build.

    I recently presented on Configuring the Spring Container at QCon San Francisco, discussing alternative configuration options and best practices. The slides are available as PDF.

  9. Back to top

    Re: Annotations vs. XML

    Nov 23, 2007 1:30 AM by Jörg Gottschling

    The annotation @Resource is a little confusing in the context of spring. As I saw it I first thought it will be used for ressources and not for beans. Like that:

    @Ressource("file:out/example.txt")
    public void setOutput(Ressource ressource) {...}

  10. Back to top

    Re: Annotations vs. XML

    Nov 26, 2007 2:34 PM by Rod Johnson

    Jorg

    I agree that @Resource is a poor name for the annotation, but we didn't choose it...

    Rgds
    Rod

  11. Back to top

    lament the passing of design and OO programming

    Nov 27, 2007 6:05 PM by James Richardson

    as now i can turn everything into a FactoryBeanImpl.

    @Bonkers(Retention.FORALLTIME)
    afterPropertiesSet()

  12. Back to top

    Great Job

    Nov 28, 2007 2:39 AM by Khaled Habiburahman

    Great Job, keep it up

    Thanks

  13. Back to top

    Re: Missing/hidden XML

    Nov 29, 2007 5:11 AM by mohan raj

    Good Artical...

  14. Back to top

    Re: Great Job

    Nov 29, 2007 5:12 AM by mohan raj

    Of course...Great

  15. Back to top

    the 2nd part and 3rd part

    Feb 5, 2008 8:43 AM by Nantian Lotus

    Good material. But where can I find the 2nd part and 3rd part of this series.
    Thanks.

  16. Back to top

    Re: Good Stuff

    Mar 4, 2008 2:53 PM by Darryl Pentz

    Not to mention how it's never mentioned how to actually create an instance of the object that is injected by Spring. We see the annotated class, and the <bean.../> configuration XML, but no idea how to construct an instance of the annotated class. Does it appear as if by magic? Is it just me, but why are there no examples of this. Everything is just assumed to originate as some implicit class of the Spring framework, like a controller or some such.

    I'd like to use this uber-magic of Spring, but how do I create instances of my own 'controller-type' classes automagically injected by Spring. Is this a state secret perhaps? Sorry, just frustrated by the lack of examples, clearly.</bean.../>

  17. Back to top

    Re: the 2nd part and 3rd part

    Mar 27, 2008 2:06 PM by Lou Sacco

    Agreed...this would be very useful.

  18. Back to top

    Re: Good Stuff

    May 12, 2008 9:40 AM by Archie Cobbs

    Darryl Pentz writes:


    Not to mention how it's never mentioned how to actually create an instance of the object that is injected by Spring. We see the annotated class, and the configuration XML, but no idea how to construct an instance of the annotated class. Does it appear as if by magic?


    I had the same question the first time I read this article (too quickly). The answer lies in the "Auto-Detection of Spring Components". The key "magic" is this tag:


    <context:component-scan ... />


    which causes Spring to automatically go hunting through your JARs looking for specially-annotated classes. When it finds them, it auto-constructs instances and adds them to your application context. The net effect is a purely annotation-driven (zero XML) way to add and configure individual beans.

  19. Back to top

    2nd and 3rd parts

    Jun 3, 2008 4:58 AM by Gregor Morrison

    Mark, great work, are parts 2 and 3 available yet?

  20. Back to top

    Re: 2nd and 3rd parts

    Aug 21, 2008 1:38 PM by Mohammad Naqvi

    Hey Greg, Did you hear anything about the 2nd and 3rd parts yet?

  21. Back to top

    the last part

    Sep 8, 2008 3:38 PM by john wang

    "While all Spring-managed objects are treated as singleton instances by default, it is sometimes necessary to specify an alternate "scope" for an object. "

    I believe with the @PostConstruct @PreDestroy, the default should change to "request" or "prototype", not singleton any more... otherwise it is very possibly not thread-safe....

    And is there any plan to support more lifecycle/AOP like annoations? like @PreInvoke @PostInvoke....

  22. Back to top

    Re: the last part

    Nov 19, 2008 6:00 AM by Lukasz Budnik

    I wrote a series of posts called "Spring for JEE developers"

    I describe some of Spring 2.5 new features like partial implementations of JSR 220 and JSR 250.

    If someone is interested here they are:

    jee-bpel-soa.blogspot.com/2008/11/spring-for-je...
    jee-bpel-soa.blogspot.com/2008/11/spring-for-je...
    jee-bpel-soa.blogspot.com/2008/11/spring-for-je...

    best regards

  23. Back to top

    Re: 2nd and 3rd parts

    Dec 10, 2008 6:03 AM by Johan Pelgrim

    Here's the second part (haven't found the third part yet)

    www.infoq.com/articles/spring-2.5-ii-spring-mvc

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.