RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Stereotype Annotations Cut Down XML Configuration in Spring

Spring stereotype annotations can greatly reduce the amount of XML configuration required to build and maintain a Spring application.

nnotations have been part of the Spring Model-View-Controller (MVC) Framework since Spring 2.0. In Spring, annotations can greatly reduce the amount of XML bean configuration and wiring needed. Given the many components of the Spring MVC environment (handler mapping, controller, view resolver, and view), XML configuration can turn unwieldy in a hurry. So, Spring MVC configuration is certainly one area that can really benefit from reduced configuration.

As of Spring 2.5, the framework added new annotations to more easily configure and assemble the components of a multi-layered application, such as you might find in an MVC-designed system. In fact, an important type of annotation added in Spring 2.5, stereotype annotations, is for configuring the Spring MVC controller components.

Are these new annotations critical to your applications? Well, Rod Johnson (Spring founder, project lead, and CEO of SpringSource) has indicated that the future of Spring MVC lies in the new Spring 2.5 annotations. In an interview with InfoQ on January 22, 2009, Johnson said:

In Spring framework 3.0 there will be a number of deprecations, for example, the old MVC controller hierarchy. We view the Annotation-based Spring MVC usage model that we introduced in Spring 2.5 as being a far superior replacement.

That makes these new annotations, in particular those associated to Spring MVC, pretty important. Thus, the purpose of this article is to help you learn what looks to be the future of Spring development.

Stereotype Annotations

The component annotations introduced in Spring 2.5 are really just a continuation of the "stereotype" annotations introduced in Spring 2.0. (The stereotype annotation that Spring 2.0 introduced was @Repository.) Stereotype annotations are markers for any class that fulfills a role within an application. This helps remove, or at least greatly reduce, the Spring XML configuration required for these components. Specifically, the roles or stereotypes defined in Spring today include Repository, Service, and Controller.

Spring also defines these stereotypes as specializations of a more generic stereotype, Component. The Component annotation allows the Spring team to create new stereotypes in future versions of the framework. It also allows you to define your own stereotype components. So Spring actually defines four stereotype annotations. Table 1 lists the four types of stereotypes and their purposes.

Table 1. The Four Types of Spring Stereotype Components and Their Purposes
Stereotype Description Annotation
Component: Generic stereotype @Component
Repository: A class that serves in the persistence layer of the application as a data access object (DAO), otherwise known as a repository in some other technologies @Repository
Service: A class that is a business service façade, often providing some reusable business processing @Service
Controller: A controller component in the presentation layer of the application, as it relates to a MVC-designed application @Controller

Per the Spring documentation, in addition to simplifying configuration and wiring, stereotype annotations make your application components "more properly suited for processing by tools or associating with aspects."

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date