Spring Hello World Application

As we have been through the basics of Spring Framework, we will check out how to create our first Hello World application.

In this Hello World application, we will basically –

  1. Create a sample bean class.
  2. Define the bean in the applicationContext file, along with setter injection of one of it properties.
  3. Get the bean instance from application context, and invoke one of the bean methods.

So let’s get started!

1. Open Eclipse, Go to File -> New -> Maven Project

New Maven Project

2. Clicking on Next, you will see the below window. Apply the filter as shown and select the appropriate Archetype – Maven Archetype Select

3. Clicking on Next asks you to set the Archetype Parameters. Do it similar to shown below –

Set Maven Archetype Parameters4. Click on Finish. You can now see the following project structure created in your eclipse work-space.
5. Since we are creating a Spring MVC application, we need to add up the dependencies accordingly into pom.xml file. Lets open pom.xml file and add entries for Spring dependencies –

6. Make sure Tomcat Server is properly installed.

7. Right click on your Maven project, go to Maven, and then click on Update Project. A window will open. Just click on the button that says OK.

8. Let’s create the HelloWorld.java bean class –

9. Let’s create applicationContext.xml file on the classpath, and add the following to it –

Note that we are setting the ‘name’ property as ‘Abhimanyu’ using Setter Based Injection. Read more on Dependency Injection

10. Now create the client class MainApp.java, from where the bean definitions will be loaded from applicationContext. We then get the helloWorld bean from the applicationContext and invoke one of its method, sayHello().

11. Now run the application as Java Application, while keeping an eye on the server console.Spring Hello World Application output

The application runs as expected and we see the proper output getting displayed on the server console.

Useful Links

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.