Spring RCP – Tutorial 1

Spring RCP Tutorial 1 – The Basics.

Note : First of all I have to tell you that what I write here is what I experienced while learning Spring RCP. I am no expert in Spring RCP and there may be mistakes and wrong do’s in this tutorial. Experts are welcome to point them out.

Introduction.

Spring RCP is a platform that can be used to develop high quality swing applications in a very short time. Since this technology is relatively new the number of tutorials about it is very small. Therefore I have decided to write my own.

Getting Started.

You will need too download the Spring RCP 0.2.1 from http://spring-rich-c.sourceforge.net/

Start a new Project in NetBeans.

  1. Open up Netbeans.
  2. Select File à New Project
  3. Enter SpringRCP-1 as the project name and select an appropriate project location.

Setting up the libraries.

  1. On Netbeans select Tools à Library Manager.
  2. Click New Library and type in the name SpringRCP_0.2.1, select library type as Class Library.
  3. Click Add Jar/ Folder and browse to the spring-richclient-full-0.2.1.jar file in the downloaded SpringRCP distribution. (spring-richclient-0.2.1\full\spring-richclient-full-0.2.1.jar).
  4. Also add the following libraries. These can be found in the Spring RCP distribution under spring-richclient-0.2.1\lib. (version and release numbers were ommited from file names to reduce confusion)
    • log4j.jar
    • spring-beans.jar
    • spring-core.jar
    • spring-context.jar
    • commons-logging.jar
    • looks.jar
  5. Click OK.
  6. Right Click on the project node on the Projects Tab and select properties.

    projectnodes1.gif

  7. Select Libraries : Add Library and add the SpringRCP_0.2.1 library which we created before to the project.

Writing the Hello World Application

First we need to create an application context xml file which will hold all bean definitions of the application.

I choose to create the application-context.xml file under src/resources/ directory of the project direcotry structure.

Create a new xml file named application-context.xml in the above folder. ( you will have to create the resources folder under the src folder first)

 

The content of the applicatoin-context.xml is as follows.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
    <bean name="application" class="org.springframework.richclient.application.Application">
        <constructor-arg>
            <ref bean="applicationLifecycleAdvisor" />
        </constructor-arg>
    </bean>
    
    <bean name="applicationLifecycleAdvisor" class="springrcp.tutorial1.HelloWorldLifecycleAdvisor">
        <property name="startingPageId">
            <value>helloWorldPage</value>
        </property>
    </bean>
    
    <bean name="helloWorldPage" class="org.springframework.richclient.application.support.DefaultViewDescriptor">
        <property name="viewClass">
            <value>springrcp.tutorial1.HelloWorldView</value>
        </property>
    </bean>
    
    <bean id="serviceLocator" class="org.springframework.richclient.application.ApplicationServicesLocator">
        <property name="applicationServices" ref="applicationServices"/>
    </bean>
    
    <bean id="applicationServices" class="org.springframework.richclient.application.support.DefaultApplicationServices">
        <property name="imageSourceId"><idref bean="imageSource"/></property>
    </bean>
    
    <bean id="imageSource" class="org.springframework.richclient.image.DefaultImageSource">
        <constructor-arg>
            <ref bean="imageResourcesFactory"/>
        </constructor-arg>
    </bean>
    
    <bean id="imageResourcesFactory" class="org.springframework.context.support.ResourceMapFactoryBean">
        <property name="location">
            <value>resources/images.properties</value>
        </property>
        <property name="resourceBasePath">
            <value>resources/images/</value>
        </property>
    </bean>
</beans>

If you are familiar with the spring framwork and AOP (don’t worry if you are not) you will notice that every thing is done through dependency injection.

I will explain each and every bean definition separately which comes the <beans> element.

<bean name="application" class="org.springframework.richclient.application.Application">
        <constructor-arg>
            <ref bean="applicationLifecycleAdvisor" />
        </constructor-arg>
</bean> 

This bean is the main bean which defines the actual application. It must be given the name application. (For the time being remember that as a rule. You will know why it is given that name later.)

If you take a look at the java doc of the class org.springframework.richclient.application.Application you will see that there are two constructors one which accepts two arguments and another with a single argument. For the moment forget about the constructor with two arguments. We’ll use it in a later tutorial.

The constructor with a single argument accepts a org.springframework.richclient.application.config.ApplicationLifecycleAdvisor.

An application lifecycle advisor is class which contains methods that will be called at different stateges of the applicatoin such as onStartup, onClose etc. (This concept is called callbacks, If you want to know more just read some articles on Aspect Oriented Programming (AOP))

<bean name="applicationLifecycleAdvisor" class="springrcp.tutorial1.HelloWorldLifecycleAdvisor">
    <property name="startingPageId">
            <value>helloWorldPage</value>
    </property>
</bean>

Since the class ApplicationLifecyclceAdvisor is abstract I’ll have to write my own life cycle advisor inheriting the above. I’ll name that class HelloworldLifecycelAdvisor.

The ApplicationLifecycelAdvisor class specifies what page / user interface should be shown when the application is started. This is specified through the property “startingPageId”. I have specified the value as helloWorldPage where it is a view descriptor bean.

HelloWorldLifecycleAdvisor.java is as follows.

package springrcp.tutorial1;
import org.springframework.richclient.application.config.ApplicationLifecycleAdvisor;
/**
 *
 * @author Ravith
 */
public class HelloWorldLifecycleAdvisor extends ApplicationLifecycleAdvisor{
    /**
     * Creates a new instance of HelloWorldLifecycleAdvisor
     */
    public HelloWorldLifecycleAdvisor() {
    }     
}

Let us now see what a view descriptor is. As the name implies it tells what are the things that make up the view / user interface.

<bean name="helloWorldPage" class="org.springframework.richclient.application.support.DefaultViewDescriptor">
    <property name="viewClass">
        <value>springrcp.tutorial1.HelloWorldView</value>
    </property>
</bean>

The HelloWorldView.java is as follows.

package springrcp.tutorial1;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import org.springframework.richclient.application.support.AbstractView;
/**
 *
 * @author Ravith
 */
public class HelloWorldView extends AbstractView{    
    /** Creates a new instance of HelloWorldView */
    public HelloWorldView() {
    }
    
    protected JComponent createControl() {
       JLabel lblHelloWorld = new JLabel();//create a new label
       lblHelloWorld.setText("Hello Spring RCP World !"); // set the text
       return lblHelloWorld; // return the label. 
    }        
}

The HelloWorldView class is a subclass of AbstractView which inturn implements the org.springframework.richclient.application.View interface. We need to override the createControl() method to return our user interface. In the above code I have overridden the method to return a simple text lable with the text “Hello Spring RCP World !”.

Now let us take a look at the remaining bean definitions.

<bean id=”serviceLocator” class=”org.springframework.richclient.application.ApplicationServicesLocator”> <property name=”applicationServices” ref=”applicationServices”/>

</bean>

This bean is responsible for locating the application services which actually do the business logic of the application. Just remember it as it is. We’ll use it in more detail when we are familiar with things.

I will use the default set of application services because we need to configure a image source for our application. (I’ve tried without this and it fails with an error.)

<bean id=”applicationServices” class=”org.springframework.richclient.application.support.DefaultApplicationServices”>

<property name=”imageSourceId”><idref bean=”imageSource”/></property>

</bean>

The bean imageSource will be responsible for mapping image keys to actual images. Don’t worry abt this right now. U’ll understand it in a minute.

<bean id=”imageSource” class=”org.springframework.richclient.image.DefaultImageSource”>

<constructor-arg>

<ref bean=”imageResourcesFactory”/>

</constructor-arg>

</bean>

<bean id=”imageResourcesFactory” class=”org.springframework.context.support.ResourceMapFactoryBean”>

<property name=”location”>

<value>resources/images.properties</value>

</property>

<property name=”resourceBasePath”>

<value>resources/images/</value>

</property>

</bean>

The imageResourcesFactory class is used to load a map of keys and image file names from a resource bundle properties file, in this case named images.properties.

The images.properties file is a simple text file with the extention .properties and contains the following line of text.

applicationInfo.image=image1.gif

The resourceBasePath property of the imageResourceFactory bean is used to append any common path used by all images of our application.

So .. that means…. You guessed it right.!!

“image1.gif” will be taken as “resources/images/image1.gif”

This is how the file structure of the application looks like now.

projectnodes2.gif

So the remaining job is to create a class that will contain the main method to execute our hello world application.

The Main.java will be as follows.

package springrcp.tutorial1;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.richclient.application.ApplicationLauncher;

/**

*

* @author Ravith

*/

public class Main {

/** Creates a new instance of Main */

public Main() {

}

public static void main(String[] args){

String appContextPath = “resources/application-context.xml”;

ApplicationContext appCtx = new ClassPathXmlApplicationContext(appContextPath);

new ApplicationLauncher(appCtx);

}

}

We need to tell the spring framework to use our application-context.xml file to configure the beans and perform the IOC and AOP functionalities. (Read some spring tutorials if you really want to understand how IOC and AOP works).

The ClassPathXmlApplicationContext is created by passing the path of the xml configuration file. This ApplicationContext is then passed in to the ApplicationLauncher which will launch the application.

I hope I didn’t leave you more confused than you started. Any way if there are parts of this tutorial that need to be explained more please drop in a comment. Also point any mistakes if there are any.

In the next tutorial we will take a look at splash screens and a little advanced scenario.

Until then you are free to mess up with this example.

 

Advertisements

10 thoughts on “Spring RCP – Tutorial 1

  1. Hi, how are you? Thank you very much for your tutorial. There aren’t too much information about Spring RCP. Could you help me to understand the next error?

    Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name ‘applicationServices’ defined in class path resource [application-context.xml]: Class that bean class [org.springframework.richclient.application.support.DefaultApplicationServices] depends on not found; nested exception is java.lang.NoClassDefFoundError
    java.lang.NoClassDefFoundError

  2. Hi Fernando, sorry for the late reply, little buzy this days, so I couldn’t visit my blog for a while.

    Any way coming to your question, I feel like you don’t have all the dependent jar files in your classpath.

    Any way a post of the stack trace would help to figure out which jar is missing…

    Cheers. and thanks for reading my blog. I’ll try to get the next tutorial up soon as I get time.

    1. Hi i am raviraj i am very new with spring rcp i am getting error (org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘application’ is defined) while running this application please help me out with solution

  3. Yes, We override the createControl method and provide our gui components, laid out using what ever layout manager we use and we return the holding JPanel/JFrame, (since they are subclasses of JComponent)

  4. Thanks for your tutorial ravith.
    Merci beaucoup.
    I’am waiting for your next tutorial.
    It was very useful for me.

    Good continuation.

  5. Hello Uditha,

    It has been some time since I wrote this tutorial, almost a year.. Little bit out of touch in this topic right now and also short of time. I’d rather recommend you to follow Chris Parson’s guides which can be found at http://pa.rsons.org/taxonomy/term/2 . Found that he has explained binding in one of his tutorials

    Hope it helps.

    Ravith

  6. Thanks a lot it was quite helpful.Slight change I had to do ..we had to place spring-binding 1.0.4 .jar version as its not able to find class ConversionService.
    Can we also some advance tutorial on this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s