Creating a Hello World program in Spring with Eclipse

Thursday, June 3, 2010

Hello World programs are usually the first steps in learning any new technology. Though the programs are simple are rarely do anything significant, they are important from two angles.
a) Your IDE becomes ready for more complex programs because of the setup/configuration you need to perform before you are able to run the Hello World program.
b) It gives you a real feel of though minuscule, how the technology works.

Thus it is important that you create a small program and execute it once you gain the preliminary knowledge of any Technology. Here is a simple program and steps for how to set it up. The IDE I have used for this example is Eclipse which is one of the most popular IDEs.

Step 1 : Create a Java Project.Click the following : File -> New -> Java Project

  • In the project name mention “SpringTest”. Click Next
  • Click the Libraries Tab. Click “Add External Jars” button.
  • Now the file browser go to the directory where Spring jars are present. Usually it is “dist” directory under the spring uninstall directory. Though you may not need to add all the jar for a hello world program, they will be useful when you create more complex examples using the more advanced features of Spring.
  • You also need asm.jar. You can download them from
  • http://download.forge.objectweb.org/asm/asm-3.2-bin.zip
  • Another library which is required is antlr. Here is the path : https://m2proxy.atlassian.com/repository/public/org/antlr/com.springsource.org.antlr/3.0.1/com.springsource.org.antlr-3.0.1.jar


Step 2 : Create the Files

a) Under the src create a file : "config.xml". Here is the content.







This xml file is the Spring configuration file which our application will use. We declare a bean with id “helloWorld” which refers to HelloWorld class.

b) Create a package. Right click on src folder and select New->Package. Name it “headfirstspring”
c) Create a Java file and name it as “HelloWorld.java” under the “headfirstspring” package. This is a simple java file with just one method sayHello(). Here is the code.

package headfirstspring;

public class HelloWorld {
public void sayHello(){
System.out.println("Hello World");
}
}


d) Create another Java file “TestHelloWorld.java” under the “headfirstspring” package. This file contains main method. In the method load we first load the configuration file which create the bean container. Next, we just access the bean from the container and call the sayHello method. Here is the code for that.

package headfirstspring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloWorld {
public static void main(String args[]){
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext("config.xml");
// retrieve configured instance
HelloWorld firstBean = (HelloWorld)context.getBean("helloWorld");
firstBean.sayHello();
}
}

Step 3 : Run the Example
This is the simplest step. Right click on “TestHelloWorld.java” -> Run As -> Java Application. You should see the following output.

0 comments:

Post a Comment

  © Blogger template On The Road by Ourblogtemplates.com 2009

Back to TOP