Dashboard > Appistry EAF v3.8 > ... > Tutorials on using the Fabric > Hello World with Spring Beans
Log In   View a printable version of the current page.  
  Hello World with Spring Beans

Introduction

To introduce using Spring beans within the fabric, we will implement a simple, Spring hello world application running within the local JVM. Then, we will deploy our HelloWorld Spring bean on the fabric, and call it using Spring remoting.

You may create your own fabric application and client application, or you may use the files and projects provided in tutorial_samples.zip and tutorial_samples.tgz archives. These files and projects are not included in the Fabric Software Development Kit (SDK) installation, but will be available for later versions. The fabric APIs are not dependent on a particular build tool or development environment (IDE), so you may use your preferred build tools. For your convenience, we have included project files for several platforms. These can be seen in the table to the right.

In this Kick Start tutorial we will

  • write a simple, HelloWorld Spring bean
  • package our HelloWorld Spring bean in a fabric application
  • deploy the fabric application, with the HelloWorld Spring bean to the fabric
  • invoke the HelloWorld Spring bean running in the fabric using a Spring remoting method call
  • display the result

Hello World Spring Client

Hello World Spring Client Application

The code for HelloWorldSpringClient is plain Java code using the Spring application context to get and call beans. There is no need to include Fabric APIs in your code. Here, our client gets both a local instance and a fabric instance of the HelloWorld Spring bean, and calls each one.

HelloWorldSpringClient.java (in tutorial_samples/java/spring_hello_world/spring_hello_world_client/com/appistry/samples/spring_hello_world/client)

01. package com.appistry.samples.spring_hello_world.client;
02. 
03. import org.springframework.context.support.ClassPathXmlApplicationContext;
04. 
05. import com.appistry.samples.spring_hello_world.bean.IHelloWorldBean;
06. 
07. public class HelloWorldSpringClient 
08. {
09.     public static void main(String args[])
10. 	{    	
11. 		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring_hello_world_bean.xml");	
12. 		
13. 		IHelloWorldBean localBean  = (IHelloWorldBean)context.getBean("localHelloWorldBean");
14. 		IHelloWorldBean fabricBean = (IHelloWorldBean)context.getBean("fabricHelloWorldBean"); 
15. 		
16. 		System.out.println(localBean.greet("locally.")); 
17. 		System.out.println(fabricBean.greet("from the fabric.")); 
18. 	}
19. }

IHelloWorldBean interface

When using Spring remoting to call a remote Spring bean instance, we are required to declare an interface. Here is our IHelloWorldBean interface.

IHelloWorldBean.java (in tutorial_samples/java/spring_hello_world/spring_hello_world_client/com/appistry/samples/spring_hello_world/bean)

1. package com.appistry.samples.spring_hello_world.bean;
2. 
3. public interface IHelloWorldBean 
4. {
5. 	public abstract String greet(String name);
6. }

Client Spring Bean XML

The following Spring application context XML is used by the Hello World Spring client application. In this Spring application context XML, we wire two definitions for our HelloWorld Spring bean. The first, localHelloWorldBean is a local instance of the bean. The second, fabricHelloWorldBean, is wired using the FabricProxyFactoryBean. The FabricProxyFactoryBean works similarly to the Spring RMI remoting proxies. It forwards calls on an interface (in this case the IHelloWorldBean interface) into the fabric, specifically to an instance of that interface running in a fabric application. Last, we wire the Fabric API bean used by the FabricProxyFactoryBean to locate and talk to the fabric.

spring_hello_world_bean.xml (in tutorial_samples/java/spring_hello_world/spring_hello_world_client)

01. <?xml version="1.0"?>
02. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
03. <beans>
04.     <bean id="localHelloWorldBean" class="com.appistry.samples.spring_hello_world.bean.HelloWorldBean"/>
05.     <bean id="fabricHelloWorldBean" class="com.appistry.spring.FabricProxyFactoryBean">
06.         <property name="serviceInterface" value="com.appistry.samples.spring_hello_world.bean.IHelloWorldBean"/>
07.         <property name="fabric">
08.             <ref local="fabric"/>
09.         </property>
10.         <property name="applicationName" value="spring_hello_world_app"/>
11.         <property name="componentName" value="hello_world_component"/>
12.     </bean>
13.     <bean id="fabric" class="com.appistry.fabric.Fabric">
14.         <constructor-arg value="239.255.0.1"/>
15.         <constructor-arg value="31000"/>
16.     </bean>
17. </beans>

Spring Hello World Fabric Application

Spring Hello World Java Bean

Below is the simple, HelloWorldBean that we are going to fabric-enable using Spring. Note that this is a plain, old, Java object (POJO), that does not require any Fabric APIs.

HelloWorldBean.java (in tutorial_samples/java/spring_hello_world/spring_hello_world_app/com/appistry/samples/spring_hello_world/bean)

1. package com.appistry.samples.spring_hello_world.bean;
2. 
3. public class HelloWorldBean implements IHelloWorldBean
4. {
5. 	public String greet(String name)
6. 	{
7. 		return "Hello World, " + name;
8. 	}
9. }

Spring configuration file for HelloWorldBean

The following Spring application context XML is used by the Hello World fabric application to load the HelloWorldBean as a Java component in the fabric. In this application context XML, we wire a local instance of our POJO. Note that this is a typical Spring bean definition with no fabric specific settings.

spring_hello_world_bean.xml (in tutorial_samples/java/spring_hello_world/spring_hello_world_app)

1. <?xml version="1.0"?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4.     <bean id="HelloWorldBean" class="com.appistry.samples.spring_hello_world.bean.HelloWorldBean"/>
5. </beans>

Spring Hello World Component XML Definition

Next, we identify the HelloWorldBean Spring bean as a fabric Java component. This is done using fabric component definition XML as seen below.

First we define a name for our component. This name can match the ID or name used in the Spring configuration file for the Spring bean. Last, we specify the spring-bean tag, using the ID or name of the Spring bean from the Spring configuration file.

spring_hello_world_component.xml (in tutorial_samples/java/spring_hello_world/spring_hello_world_app)

1. <?xml version="1.0" encoding="utf-8"?>
2. <java-components xmlns="http://www.appistry.com/ns/component"
3. 	             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4.                  xsi:schemaLocation="http://www.appistry.com/ns/component eaf-component.xsd">
5.     <component name="hello_world_component" default-timeout="5">
6.         <spring-bean name="HelloWorldBean"/>
7.     </component>
8. </java-components>
Spring bean

Identifies the bean name that you want this component to reference

Spring Bean Factory

Appistry EAF uses the Spring BeanFactoryLocator pattern to locate and load your Spring application contexts. We do not specify that you must use one ApplicationContext type or another. That decision is yours. To do this, you provide an application context XML file defining a bean for the type of Spring ApplicationContext implementation you want to use. The beanRefFactory.xml file below defines a factory bean "myBeanFactory" of type ClassPathXmlApplicationContext.

beanRefFactory.xml (in tutorial_samples/java/spring_hello_world/spring_hello_world_app)

1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3. <beans>
4.     <bean id="myBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
5.         <constructor-arg value="spring_hello_world_bean.xml"/>
6.     </bean>
7. </beans>

Spring Hello World Application XML Definition

Now we tie together all the pieces of our Spring fabric application using Fabric Application Definition XML. First, we tell the fabric how to load our Spring application context using the <spring> tag. Second, we include our Java Spring component using the <components> tag. Last, we list the jars loaded by our fabric application using the <java-libs> tag.

spring_hello_world_app.xml (in tutorial_samples/java/spring_hello_world/spring_hello_world_app)

01. <?xml version="1.0"?>
02. <!DOCTYPE app SYSTEM "FabricApp.dtd">
03. <app name="spring_hello_world_app" version="4.2">
04.     <spring>
05.         <bean-factory-locator bean="myBeanFactory" resource-location="beanRefFactory.xml"/>
06.     </spring>
07.     <components>
08.         <file name="spring_hello_world_component.xml"/>
09.     </components>
10.     <java-libs>
11.         <file name="spring_hello_world_bean.jar"/>
12.         <file name="spring.jar"/>
13.         <file name="commons-logging.jar"/>
14.     </java-libs>
15. </app>
Do I have to include spring.jar (and other dependencies) in each of my Fabric Spring applications?

Actually you do not need to do that. We have done it here for the sake of simplicity. However, you can bundle up jars into Fabric Archives, and deploy them as versioned libraries that multiple fabric applications can reference and share.

Building, packaging and deploying the Spring Hello World Fabric Application

After building the fabric application, you must create the spring_hello_world_bean.jar
  • If you build using the provided ant build scripts then, by default, this jar will be created for you.
  • If you create the jar file by other means, then the spring_hello_world_bean.jar must include the following files.
    • com/appistry/samples/spring_hello_world/bean/HelloWorldBean.class
    • beanRefFactory.xml
    • spring_hello_world_bean.xml
The Building, Packaging, and Deploying a Fabric Application tutorial gives directions for building, packaging and deploying fabric applications.

Running the Spring Hello World Fabric Application

Once the fabric application is deployed, run the com.appistry.samples.spring_hello_world/client/HelloWorldSpringClient main.

If you are using ant, then you can execute the "ant run" target.