Custom components with Spring 2.5

A hands-on introduction to Spring's flexible component model

Catch up to Spring, with this hands-on introduction to Spring's custom namespaces, which make it easier than ever to build and deploy custom components in your Spring 2.x environment.

Spring is an open source framework that has been embraced by many Java developers as a means to simplify server-side Java application development. The Spring framework has evolved to be a comprehensive platform for building Java applications and services across the entire spectrum of enterprise needs. Much of Spring's popularity is due to its ability to hide infrastructure details from developers, narrowing the focus to just the task at hand.

Enterprise developers use Spring technologies and concepts like inversion of control (IOC), aspect-oriented programming (AOP), and dependency injection to implement business and application logic components as simple Java beans. Relationships between beans are defined using configuration settings and runtime introspection.

Spring 2.0 extended the framework's approach to simplicity by providing more sophisticated introspection techniques and annotation support. Spring 2.5, released in November of last year, introduced the ability to define and demarcate components using XSD data.

In this article, I introduce the new features in Spring 2.5 by showing you how to use Spring's custom namespaces and flexible component model to create and deploy a custom component in a Spring environment. I also introduce several of Spring's pre-defined custom namespaces and show you how to use Java 5 annotations to enable your Spring components for easy detection and deployment in any Spring environment.

Custom namespaces and Spring's flexible component model

Spring 2.0 introduced the ability to define custom XML namespaces. Similar to Java packages, custom namespaces make it possible to define components without concern of colliding with other named components. Spring ships with a few of its own namespaces defined, such as jee, aop, util, and others, which are discussed later in the article.

In Spring 2.0, custom namespaces are handled using namespace-handling classes, supplied by third-party vendors, that generate metadata to be consumed by the Spring framework. This metadata is processed by parser classes that are also supplied by a component developer.

Spring 2.5 introduced a flexible component model that allows the use of annotations to configure components. Developers using the Spring 2.5 component model can enable any Spring-managed object as a JMX MBean. Spring 2.5 also supports the OSGi Service Platform and interacts with the Service Component Architecture (SCA) standard.

Together, new features introduced in Spring 2.0 and Spring 2.5 have greatly simplified Spring development. In the next sections I'll walk through a Spring development scenario consisting of the following steps:

  1. Create an XSD file that defines a component.
  2. Write the Java code encapsulating the logic for the component.
  3. Create a namespace handler that maps the schema to the beans.
  4. Create a bean definition parser to handle the parsing of bean configuration data.
  5. Create two deployment-descriptor property files.
  6. Package it all up as a .jar file.

At the end of this process we'll have a basic component that can be distributed and deployed to a Spring 2.x environment as a .jar file and used within a Spring application context.

1 2 3 4 5 Page 1