Spring

Spring - Hello World Example

Spring - Hello World Example

To compose a straightforward Spring Application, which will print "Hi World!" or some other message dependent on the arrangement done in the Spring Beans Configuration record. 

#1 - Create Java Project 

The initial step is to make a straightforward Java Project utilizing Eclipse IDE. Follow the alternative File → New → Project. Lastly, select the Java Project wizard from the wizard list. Presently name your undertaking as HelloWorld utilizing the wizard window. The Project Explorer window will show up. 

#2 - Add Required Libraries 

As a subsequent advance, let us add Spring Framework and standard logging API libraries in our undertaking. To do this, right-click on your task name HelloWorld and afterward follow the accompanying choice accessible in the setting menu − Build Path → Configure Build Path to show the Java Build Path window 

#3 - Create Source Files 

Presently let us make real source documents under the HelloWorld project. First, we need to make a bundle called SpringDemo. To do this, right snap-on src in bundle traveler area and follow the choice − New → Package. 

#4 - Create Bean Configuration File 

You need to make a Bean Configuration document, an XML record, and goes about as concrete that sticks the beans, for example, the classes together. This document should be made under the src index. 

#5 - Running the Program 

When you are finished making the source and beans arrangement records, you are prepared for this progression, aggregating and running your program. To do this, keep MainApp.Java document tab dynamic and utilize either Run choice accessible in the Eclipse IDE or use Ctrl + F11 to aggregate and run your MainApp application.

HelloWorld code:

package SpringDemo;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println(message);
   }
}
The code of MainApp
package SpringDemo;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}