Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Custom components with Spring 2.5

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

  • Print
  • Feedback

Page 5 of 5

Spring's lang schema

The lang schema defines components for handling tasks related to exposing objects written in dynamic languages such as Groovy and JRuby.

The preamble entries for the lang namespace are specified as shown in bold in the following snippet:

Listing 9. Excerpt of the lang namespace

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
             http://www.springframework.org/schema/lang
            http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

<!-- <bean/> definitions here -->

</beans>

See the Resources section for more information about the lang schema.

Spring's aop schema

The aop schema defines components for handling tasks related to AOP configuration in Spring, including Spring's proxy-based framework, and integrating Spring with AspectJ.

The preamble entries for the aop namespace are specified as shown in bold in the following snippet:

Listing 10. Excerpt of the aop namespace

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<!-- <bean/> definitions here -->

</beans>

See the Resources section for more information about the aop schema.

Spring's tx schema

The tx schema defines components that provide support for transactions in the Spring framework.

The preamble entries for the tx namespace are specified as shown in bold in the following snippet:

Listing 11. Excerpt of the tx namespace

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/util/spring-tx-2.0.xsd"> 

<!-- <bean/> definitions here -->

</beans>

See the Resources section for more information about the tx schema.

Classpath scanning for components

Spring 2.5 provides support for scanning the classpath in order to identify components. You can facilitate scanning by applying the proprietary stereotype annotation @Component to a given class.

The @Component annotation specifies that the annotated class is a component, as Listing 12 shows.

Listing 12. Enable component scanning

package com.jeffhanson.spring.beans;

@Component
public class Foobar implements IHelloWorld
{
  private String message = "Hello World!";
  
  public void setMessage(String message)
  {
    this.message = message;
  }

  public String getMessage()
  {
    return message;
  }
  
  public void sayHello()
  {
    System.out.println(message);
  }
}

When Spring discovers components using classpath scanning, the bean definitions are generated for the components, which are automatically registered with Spring. Note the following configuration file:

Listing 13. Declarations to enable component scanning with Spring

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.jeffhanson.spring.beans" />

<!-- <bean/> definitions here -->
  
</beans>

The <context:component-scan annotation declaration in Listing 12 enables autodetection of any annotated classes in the package com.jeffhanson.spring.beans.

Classpath scanning simplifies the work needed to declare your classes as components in the Spring framework. This mechanism is most effective when you do not anticipate frequent changes to the relationships between beans and/or components.

In conclusion

The Spring framework is an open source staple for many Java server-side developers, and one of the foremost platforms for building Java applications and services for the enterprise. Spring's popularity is largely owed to the fact that it hides a great deal of infrastructure detail, allowing developers to focus primarily on immediate tasks. Spring's simplicity is facilitated by its pervasive use of inversion of control, aspect-oriented programming, and dependency injection for simple Java beans.

The Spring 2.0 and Spring 2.5 releases have further promoted the simplicity of Spring. In this article you have walked through a Spring component development scenario using some of the new features found in Spring 2.0 and Spring 2.5, including custom namespaces and support for XSD data and Java 5 annotations. You've also seen several of the new built-in schemas found in Spring 2.5, for custom components that handle common programming tasks such as AOP integration and transaction handling.

About the author

Jeff Hanson has more than 20 years experience as a software engineer, including working as senior engineer for the Windows OpenDoc project and as chief architect for the Zareus SOA platform. The author of numerous articles and books, he is the chief architect for Max Software Inc., where he leads design and implementation teams building desktop and server applications for the content-control industry using C++, PHP, and Java EE.
  • Print
  • Feedback

Resources