September 12, 2009

"HelloWorld" Example using Spring IDE

Spring IDE is a plug-in provided in eclipse. It can be directly used to make Spring Project just like Java Project.Following steps show how to use Spring IDE for making a simple "HelloWorld" example.
STEP # 1: Installing Spring IDE plug-in in eclipse. Go to Help Menu, Select “Software Updates”/”Install New Softwares” option.
STEP # 2: Enter following link http://springide.org/updatesite and then click on “Add” button and select all the checkboxes of Spring IDE features shown in table below. Click Next->Next-Finish until the installation process is completed.
After successful execution of Step #2 , check if following “Spring” option is shown in Project list or not.Navigation: File Menu -> Project. If it is shown, Please continue with the next steps.

STEP # 3: Click on “Next” shown in above Fig. Enter Project Name as “SpringProject”.
Click “Finish” tab. The project structure would look like following and “S” indicates that it is a Spring Project.

STEP # 4: Right click “src” icon in above Project Structure and create a package named as “com.myspring”
Click “Finish” tab.

STEP  #5 : Now right click on package created in above step and create a “HelloWorld.java” class.

STEP # 6: Add “message” property and its setter which will  be used as a “Setter Injection” and then our own method “printMessage()” in HelloWorld.java file such that it looks like following. Instead of putting hard coded message in message field, note we are using Dependency Injection design pattern provided by Spring. It will allow message to take its value from configuration files. For more details about it, right now wait for the post to come for it.

STEP # 7: So now, we have created “HelloWorld” bean in actual in previous step. Now we need to configure it in Configuration file so that whenever the Spring IoC(Inversion of Control) container loads then that bean is loaded. Navigation: File Menu->New ->Other> Spring-> Spring Bean Configuration File .
Click “Next”. Now choose path as “SpringProject/src”  and name configuration file as “beans.xml” .
Click Next –>Next and Finally “Finish”.Make sure beans check box is selected in following screen.
Click “Next” and then “Finish”. Now Spring configuration file is created. Add entry for our created bean so that beans.xml looks like following
  • id” attribute  -> it gives a unique name to the bean.
  •  “class” attribute- > it gives fully qualified class name of the bean.
  • property” element -> sets the value of the “message” property/field used in Bean class. If we want to change the different message then we need to change only the message in configuration file. This is good application of a loosely coupled application.
STEP # 8: Now to print Message , we will create a standalone java program  “PrintHelloWorld.java” file in the same way we created “HelloWorld.java” but before creating it, Please add following jar files in the BuildPath of SpringProject.
antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.context.support-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3

STEP # 9: Now create “PrintHelloWorld.java” file. It should look like following
  • Line 8: Spring IoC Container is instantiated using “beans.xml” configuration file.
  • Line 9: getBean() method is used to get the bean with id “helloWorld” from the application context.
  • Line 10: Finally printMessage() method of “HelloWorld” bean retrieved in Line 9 is called to actually print the “Hello World !”  message set in configuration file.
STEP # 10: Run “PrintHelloWorld.java” and the output will be shown like following

Feel free to correct and fire questions.
Thanks ! :)