Annotation-Driven Dependency Injection in Spring 2.1

Engineering | Mark Fisher | May 14, 2007 | ...

Spring 2.0 introduced annotation support and annotation-aware configuration options that can be leveraged by Spring users who are developing with Java 5 (or later versions):

@Transactional for demarcating and configuring transaction definitions
@Aspect (AspectJ) for defining aspects along with @Pointcut definitions and advice (@Before, @After, @Around)
@Repository for indicating a class that is operating as a repository (a.k.a. Data Access Object or DAO)
@Required for enforcing annotated bean properties are provided a value

With Spring 2.1, this theme of annotation-driven configuration has been significantly extended and will continue to evolve as we progress toward the RC1 release. In fact, it is now possible to drive Spring's dependency injection via annotations. Furthermore, Spring can discover beans that need to be configured within an application context.

This blog entry will serve as a tutorial-style introduction to the basic features in 10 easy-to-follow steps. I will follow up later in the week with information on some more advanced features and customization options. If you are interested in alternative configuration options, you should also check out the Spring Java Configuration project and this blog.

This tutorial requires at least Java 5, and Java 6 is recommended (otherwise there is a single requirement at the end of step 1).

Step 1:

Grab spring-framework-2.1-m1-with-dependencies.zip. After extracting the archive, you will find the spring.jar and spring-mock.jar in the 'dist' directory. Add them to your CLASSPATH as well as the following (paths shown are relative to the 'lib' directory of the extracted 2.1-m1 archive):

  • asm/asm-2.2.3.jar
  • asm/asm-commons-2.2.3.jar
  • aspectj/aspectjweaver.jar
  • hsqldb/hsqldb.jar
  • jakarta-commons/commons-logging.jar
  • log4j/log4j-1.2.14.jar
(NOTE: If you are not running on Java 6, you will also need to add j2ee/common-annotations.jar)

Step 2:

Provide the interfaces and classes for the example. I have tried to keep it as simple as possible yet capable of demonstrating the main functionality. I am including all of the code and configuration in a single "blog" package. I would encourage following that same guideline so that the examples work as-is; otherwise, be sure to make the necessary modifications. First, the GreetingService interface:

public interface GreetingService {
    String greet(String name);
}

Then, a simple implementation:


public class GreetingServiceImpl implements GreetingService {
    private MessageRepository messageRepository;

    public void setMessageRepository(MessageRepository messageRepository) {
        this.messageRepository = messageRepository;
    }

    public String greet(String name) {
        Locale locale = Locale.getDefault();
        String message = messageRepository.getMessage(locale.getDisplayLanguage());
        return message + " " + name;
    }
}

Since the service depends upon a MessageRepository, define…

Spring Framework 2.1 M1 released

Releases | Juergen Hoeller | May 14, 2007 | ...

Dear Spring Community,

We are pleased to announce that Spring 2.1 M1 has been released.  This is the first milestone release in the Spring 2.1 series, introducing major new features including annotation-based configuration, JCA-based message endpoint management, new "context" and "jms" XML configuration namespaces, and JDK 1.6 and Java EE 5 support.

Spring 2.1 M1 Released

 
See the associated press release for an overview of the major themes of the 2.1 release. Subscribe to the Interface21 Team Blog for discussion and examples of the new features.

Please see the changelog and JIRA…

Spring LDAP 1.2 RC1 released

Releases | Ulrik Sandberg | May 13, 2007 | ...

Dear Spring community,

We are pleased to announce the first release candidate of Spring LDAP 1.2, with a number of features and bug fixes. Only the most important are listed here. For a complete listing, please see the changelog. The release is available for download here.

  • Implemented client-side transaction support for Spring LDAP. See reference documentation for further information (LDAP-29).
  • Changed the exception hierarchy to be an unchecked mirror of the JNDI NamingException hierarchy (LDAP-4).
  • Exceptions thrown by Spring LDAP are now always Serializable, regardless of whether the wrapped NamingException is (which is not always the case) (LDAP-14).
  • Rewrote LdapEncoder.nameDecode() to solve problem with national characters and remove regular expression used in parsing, drastically improving Distinguished Name parsing performance as a bonus (LDAP-30).
  • Upgraded to Spring 2.0.4 internally. Spring 1.2.x is still supported (LDAP-35, LDAP-51).

Note that a number of API-breaking changes have been made in this release, mainly package restructuring stuff. Consequently, this is NOT a drop-in replacement for Spring LDAP 1.1.2, though upgrading should not present all that much work. Please see the supplied upgrade guide for details.

The Spring LDAP Team

Spring Web Flow Bean Scopes and JSF

Engineering | Ben Hale | May 08, 2007 | ...

I've recently finished up an interesting issue in Spring Web Flow. This issue (SWF-163) dealt with adding Spring 2.0 bean scoping support for Spring Web Flow's internal scopes. The implementation isn't really that interesting (the Scope interface is pretty easy to implement after all), but I wanted to mention exactly how you would use something like this in your application.

Spring 2.0 Scoping

In Spring 1.x, we had the idea of singleton and prototype bean scopes, but the notation was fixed and not especially descriptive with singleton="[true | false]". So in Spring 2.0, this notation was removed from the XSD style of configuration and now you see a notation that is more clear with scope="[singleton | prototype | ...]". Spring itself adds three more bean scopes; request, session, and globalSession which are related to web applications.

With the latest snapshots of Spring Web Flow 1.1, we now see bean scopes for the three major Web Flow scopes, flash, flow, and conversation.


<bean id="sale" class="org.springframework.webflow.samples.sellitem.Sale…

Spring Framework 2.0.5 released

Releases | Juergen Hoeller | May 08, 2007 | ...

Dear Spring Community,

We are pleased to announce that Spring 2.0.5 has been released.  This is is a bugfix and enhancement release in the Spring 2.0 series, addressing all issues reported since 2.0.4 and introducing further concurrency improvements. We recommend to upgrade to Spring 2.0.5 from all previous 2.0.x releases.

Spring 2.0 Released

 

Please see the changelog and JIRA roadmap for all the details of the 63 issues addressed in this release.

Juergen Hoeller
Lead, Spring Framework Development
Interface21 - http://www.interface21.com

 

Spring Batch

Engineering | Dave Syer | May 07, 2007 | ...

Introduction

I've been working hard with a couple of clients on a new product called Spring Batch. The aim is to provide tools and applications to support bulk processing in an enterprise environment. Spring Batch is part of the Spring Portfolio with an initial release in the Spring 2.1 release train.

The original impetus to build some prototype code actually came independently from a number of Interface21 clients. This provides some useful additional detail and some constraints on the implementation so that it can be applied to the real-world problems posed by the clients. I hope that this article…

Power Combination: SCA, OSGi, and Spring

Engineering | Adrian Colyer | May 01, 2007 | ...

No, that's not my headline, it's actually the title of a white paper recently published by Open SOA collaboration. To quote from the news announcement accompanying the whitepaper:

"Based upon user feedback, the OSOA Collaboration are publishing a white paper highlighting the powerful combination of the SCA, Spring and OSGi technologies aimed to help Developers simplify the creation and composition of services critical to building applications based on an SOA approach."
The Open SOA collaboration develops the Service Collaboration Architecture (SCA) specification, with partners including BEA, IBM, IONA, Oracle, Red Hat, SAP, Siemens, Sun, Tibco, and others. So when this group starts to rally around the "powerful combination of SCA, Spring, and OSGi" it's a great endorsement of the Spring Framework and of the work that we're doing in the Spring OSGi project.

The white paper provides a short overview of SCA, OSGi and Spring, and then describes how they can be used together. Quoting from the summary:

"SCA, OSGi and Spring are all useful and powerful facilities for the Java programmer to use. In the new service-oriented world that we are entering, using SCA, OSGi and Spring together provide powerful capabilities for building service implementations from sets of simple Java Beans using few APIs, with managed dependencies, version control and dynamic update capabilities, allied to the capability to compose those implementations with other service components written in Java or in other languages and existing in a distributed network of systems using a range of communication methods.

Simplicity, flexibility, manageability, testability, reusability. A key combination for enterprise developers."

I'll be co-presenting on SCA and Spring with Mike Edwards of IBM at the JavaOne conference next week: session TS-8194, "Spring and Service Component Architecture…

Querying and Downloading from Amazon S3

Engineering | Ben Hale | April 30, 2007 | ...

In a previous post, I described how we use a custom ANT task to upload nightly snapshots from the ANT based projects in the Spring portfolio. In this post I'll describe how we use Amazon S3 to generate pages for the snapshots from each project and allow users to download the snapshots.

As I mentioned in the previous post, S3 is primarily used as a REST-ful service. This means that while I used Java for the upload portion, I was free to use other languages for the download portion. I chose to use PHP in this case because it was already available on the server I was working with, and was the…

Uploading to Amazon S3 using a custom ANT task

Engineering | Ben Hale | April 25, 2007 | ...

One of the interesting side effects of a solid CI structure is that when things are running reliably, new problems start to crop up. Shortly after Spring's CI system started running smoothly, our occasional space and bandwidth issues on static.springframework.org became more pronounced. Colin Sampaleanu had done research earlier on how to alleviate some of these problems and had settled on Amazon S3.

Amazon S3 is part of the Amazon Web Services umbrella and provides an incredibly cheap online file storage service. What does 'incredibly cheap' mean? Well, from the website, it appears that…

The Essence of Spring

Engineering | Rossen Stoyanchev | April 24, 2007 | ...

This happened in Atlanta last week while I was in a Barnes & Noble bookstore. I circled around to the computer section and began scanning titles. With my head tilted I overheard a conversation about a job. I wasn't actively listening but I knew one side was pitching a job while the other was inquiring about it.

A couple of minutes later it was just me and the guy who was looking for talent. I was sure he would start speaking. Soon after he said 'so you're in J2EE?' and so the conversation began. He asked me about my work. He didn't know about Interface21 but upon hearing it's the company…

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all