Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 4 of 5
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).
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.
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.
<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.
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 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 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.
<?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.
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:
<?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.
DefaultBeanDefinitionDocumentReader for handling custom namespaces in a Spring XML configuration file.
Archived Discussions (Read only)