Spring Hello World Example


Let us begin genuine programming with Spring Framework. Before you begin composing your first model utilizing Spring structure, you need to ensure that you have set up your Spring condition appropriately as clarified in Spring - Environment Setup Chapter. We likewise accept that you have a touch of working information on Eclipse IDE.

Now let us continue to compose a straightforward Spring Application, which will print "Hi World!" or some other message dependent on the design done in Spring Beans Configuration file.

Step 1 - Create Java Project

The initial step is to make a straightforward Java Project utilizing Eclipse IDE. Pursue the alternative File → New → Project lastly select Java Project wizard from the wizard list. Presently name your venture as HelloSpring utilizing the wizard window as pursues −

Hello Spring Wizard

Once your undertaking is made effectively, you will have the accompanying substance in your Project Explorer

Hello Spring Directories

Step 2 - Add Required Libraries

As a second step let us include Spring Framework and regular logging API libraries in our venture. To do this, right-click on your task name HelloSpring and after that pursue the accompanying choice accessible in the setting menu − Build Path → Configure Build Path to show the Java Build Path window as pursues −

Java Build Path

Now use Add External JARs catch accessible under the Libraries tab to include the accompanying center JARs from Spring Framework and Common Logging establishment indexes −

  • commons-logging-1.1.1

  • spring-aop-4.1.6.RELEASE

  • spring-angles 4.1.6.RELEASE

  • spring-beans-4.1.6.RELEASE

  • spring-setting 4.1.6.RELEASE

  • spring-setting support-4.1.6.RELEASE

  • spring-center 4.1.6.RELEASE

  • spring-articulation 4.1.6.RELEASE

  • spring-instrument-4.1.6.RELEASE

  • spring-instrument-tomcat-4.1.6.RELEASE

  • spring-jdbc-4.1.6.RELEASE

  • spring-jms-4.1.6.RELEASE

  • spring-informing 4.1.6.RELEASE

  • spring-orm-4.1.6.RELEASE

  • spring-oxm-4.1.6.RELEASE

  • spring-test-4.1.6.RELEASE

  • spring-tx-4.1.6.RELEASE

  • spring-web-4.1.6.RELEASE

  • spring-webmvc-4.1.6.RELEASE

  • spring-webmvc-portlet-4.1.6.RELEASE

  • spring-websocket-4.1.6.RELEASE

Step 3 - Create Source Files

Now let us make genuine source documents under the HelloSpring venture. First we have to make a bundle called com.javacodegeeks. To do this, right snap on src in bundle voyager segment and pursue the alternative − New → Package.

Next we will make HelloWorld.java and MainApp.java documents under the com.javacodegeeks package.

Here is the substance of HelloWorld.java document −

package com.javacodegeeks; 

public class HelloWorld { 

private String message; 

public void setMessage(String message){ 

this.message = message; 

} 

public void getMessage(){ 

System.out.println("Your Message : " + message); 

} 

}

Following is the substance of the second record MainApp.java

package com.javacodegeeks; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ApplicationContext setting = new ClassPathXmlApplicationContext("Beans.xml"); 

HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); 

obj.getMessage(); 

} 

}

Following two critical focuses are to be noted about the primary program −

  • The initial step is to make an application setting where we utilized structure API ClassPathXmlApplicationContext(). This API loads beans design record and in the end dependent on the gave API, it deals with making and instating every one of the items, for example beans referenced in the arrangement file.

  • The second step is utilized to get the required bean utilizing getBean() strategy for the made setting. This technique utilizes bean ID to restore a nonexclusive item, which at last can be threw to the real article. When you have an article, you can utilize this item to call any class method.

Step 4 - Create Bean Configuration File

You need to make a Bean Configuration record which is a XML document and goes about as a bond that sticks the beans, for example the classes together. This document should be made under the src index as appeared in the accompanying screen capture −

Usually designers name this record as Beans.xml, yet you are free to pick any name you like. You need to ensure that this document is accessible in CLASSPATH and utilize a similar name in the primary application while making an application setting as appeared in MainApp.java file.

The Beans.xml is utilized to appoint novel IDs to various beans and to control the formation of articles with various qualities without affecting any of the Spring source documents. For instance, utilizing the accompanying document you can pass any an incentive for "message" variable and you can print diverse estimations of message without affecting HelloWorld.java and MainApp.java records. Give us a chance to perceive how it functions −

<?xml rendition = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/construction/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/construction/beans 

http://www.springframework.org/construction/beans/spring-beans-3.0.xsd"> 

<bean id = "helloWorld" class = "com.javacodegeeks.HelloWorld"> 

<property name = "message" value = "Hello World!"/> 

</bean> 

</beans>

When Spring application gets stacked into the memory, Framework makes utilization of the above design record to make every one of the beans characterized and doles out them a one of a kind ID as characterized in <bean> tag. You can utilize <property> tag to pass the estimations of various factors utilized at the season of article creation.

Step 5 - Running the Program

Once you are finished with making the source and beans design records, you are prepared for this progression, which is arranging and running your program. To do this, keep MainApp.Java record tab dynamic and use either Run choice accessible in the Eclipse IDE or use Ctrl + F11 to aggregate and run your MainApp application. In the event that all is well with your application, this will print the accompanying message in Eclipse IDE's comfort −

Your Message : Hello World! 

Congratulations, you have effectively made your first Spring Application. You can see the adaptability of the above Spring application by changing the estimation of "message" property and keeping both the source documents unchanged.







© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3