First Application using Spring

Now it is time to create first Spring application call Hello Spring World. The following steps describe to create first application using Spring library. The following software are required to develop first application. I am using Spring org.springframework-3.1.2.RELEASE version.

The following software are required :

Follow the below steps to create Java application using Spring

  • Create a Java project HelloWorldSpring
  • Add required jar files
  • Create java source code
  • Create Spring configuration file
  • Create client application
  • Run client application
Step 1 : Create a Java project HelloWorldSpring.
First of all, you create a Java Project in Eclipse : File -> New -> Java Project -> and specify the project name HelloWorldSpring as below screen.



Step 2 : Add required jar files.
Click Next and add the required jar files in Library tab as below :

Note You can also add jar files after creating java project through Java Build Path in Project Properties.

The commons-logging-1.0.4.jar file is not part of Spring library bundle. You need to download separately from apache site.

This step will create a java project as below :


Step 3: Create java source code :
HelloSpring.java
package pkg1.techmyguru.first;
public class HelloSpring {
  public void welcomeMessage() 
  {
  System.out.println("Hello World Spring");
  }
}

Step 4: Create spring configuration file :
Right click on project - > New -> Spring Bean Configuration File -> specify the configuration file name beans -> Next -> Next -> Finish. And the code as below
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="helloBean" class="pkg1.techmyguru.first.HelloSpring" />
</beans>

Step 5: Create client application :
HelloSpringClient.java
package pkg1.techmyguru.first;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpringClient 
{
public static void main(String[] args
{
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
HelloSpring helloSpring=ctx.getBean("helloBean",HelloSpring.class);
helloSpring.welcomeMessage();
}
}

Step 6: Run client application.
Right click on HelloSpringClient and Run As -> Java Application and see output :



Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code