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 4 of 5

Packaging a Spring component in a .jar file

To package the component, create a .jar file containing the XSD file, compiled Java bean classes and ancillary classes, and deployment descriptor files.

The directory structure for the component .jar file will look as shown in Figure 1 (click for a larger image).

Image of the directory structure for the component JAR file.

Figure 1. Directory structure for the component .jar file

Now the .jar file for the component can be placed in the Spring classpath and the component made available for use by a Spring application context.

Using a custom component in a Spring context

In order to make use of components defined by a custom namespace, you have to alter the Spring context configuration file slightly. Instead of referencing the Spring beans DTD in the context file like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

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

you'll apply the XSD for your custom namespace. You apply the XSD file to the Spring context using standard XML namespace and schema-definition preamble, as shown in Listing 5.

Listing 5. Spring configuration file with custom namespace definition

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:fbar="http://www.jeffhanson.com/schema/service/foobar"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                     http://www.jeffhanson.com/schema/service/foobar
                           http://www.jeffhanson.com/schema/service/foobar/foobar.xsd">

  <bean id="testbean" class="com.jeffhanson.spring.app.TestBean">
    <property name="messenger">
      <fbar:component id="foobarbean" message="Ciao baby!" />
    </property>
  </bean>
</beans>

Now your component will be detected by the Spring container and can be used by any application or service in a Spring 2.x environment. Listing 6 illustrates the use of the standard Spring XmlBeanFactory to load and use the custom component.

Listing 6. Spring XmlBeanFactory loads the custom component

public static void main(String[] args)
  throws Exception
{
  BeanFactory factory = new XmlBeanFactory(new FileSystemResource("testapp.xml"));

  com.jeffhanson.spring.app.TestBean bean =
    (com.jeffhanson.spring.app.TestBean)factory.getBean("testbean");
  com.jeffhanson.spring.beans.Foobar messenger =
    (com.jeffhanson.spring.beans.Foobar)bean.getMessenger();
  System.out.println("Messenger says...");
  messenger.sayHello();
}

Spring's pre-defined custom namespaces

Spring ships with a number of custom namespaces already defined with a palette of components defined in each. These can be consumed in the same manner as above. I'll introduce some of these components in the next sections.

Spring's util schema

Spring provides a set of custom components that deal with tasks that are encountered in many common Java and Java EE programming situations.

The util schema defines components for handling configuration duties such as configuring collections, referencing constants, and so on.

The preamble entries for the util namespace are specified as shown in bold in Listing 7.

Listing 7. Excerpt of the util 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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
             http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util-2.0.xsd">

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

</beans>

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

Spring's jee schema

The jee schema defines components for handling Java EE-related configuration tasks, such as defining EJB references and performing JNDI lookups.

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

Listing 8. Excerpt of the jee 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:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/jee
             http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

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

</beans>

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

  • Print
  • Feedback

Resources