In the previous tutorials, we have already discussed the Spring Introduction, Spring Architecture and Spring Environmental Setup. Now that we are clear on those topics we can move forward. Keeping in mind the basic programming practices, today in this tutorial we are going to create a sample spring based Hello world application.
Follow the steps to create the application:
Step1
–
Create a java project
Using Eclipse create a new
java project. Java project can be created by File
à New à Project à Java
Project. A wizard window will open
where we have to give the name to the project. Enter the name as HelloWorld.
Below is the screenshot
After the project is created
you will see below entries
Step2 – Add
required Libraries
Now that the sample java application
is created, we must add the Spring framework and common logging jars into the project.
To do that Right-click
on HelloWorld à Build Path Ã
Configure Build Path à Libraries tab.
On the left side, click on Add External Jars and add the below jars by downloading them manually.
1.
Commons-loggin-1.1.1
2.
Spring-aop-4.1.6.RELEASE
3.
Spring-aspects-4.1.6.RELEASE
4.
Spring-beans-4.1.6.RELEASE
5.
Spring-context-4.1.6.RELEASE
6.
Spring-context-support-4.1.6.RELEASE
7.
Spring-core-4.1.6.RELEASE
8.
Spring-expression-4.1.6.RELEASE
9.
Spring-instrument-4.1.6.RELEASE
10. Spring-instrument-tomcat-4.1.6.RELEASE
11. Spring-jdbc-4.1.6.RELEASE
12. Spring-jms-4.1.6.RELEASE
13. Spring-messaging-4.1.6.RELEASE
14. Spring-orm-4.1.6.RELEASE
15. Spring-oxm-4.1.6.RELEASE
16. Spring-test-4.1.6.RELEASE
17. Spring-tx-4.1.6.RELEASE
18. Spring-web-4.1.6.RELEASE
19. Spring-webmvc-4.1.6.RELEASE
20. Spring-webmvc-portlet-4.1.6.RELEASE
21. Spring-websocket-4.1.6.RELEASE
Step 3 –
Source file creation
Now the application
configurations are ready. We can jump into coding. First, create the desired
package ( for example com.java8bysourabhrai ), right-click
on the src à New à package.
Create HelloWorld.java and MainApp.java under com.java8bysourabhrai.
Write the following code
into these files:
HelloWorld.java
MainApp.java
There are two main class
that we have seen in the above code, these class are:
1.
ApplicationContext
– Using ApplicationContext we can implement IoC in
spring. This we will see in further chapters. ClassPathXmlApplicationContext()
is the implementation of ApplicationContext. This takes
the bean name and load the bean from the configuration file and takes care of
the object creation of the class.
2.
getBean()
– this method uses the bean id to get the bean
reference. The return type of the method is the object so, we need to typecast
the object into the required bean object. This object can then be used to call
the class methods.
Step
4 – Creating the bean configuration file
This file is an XML file which will act as the configuration
file. This configuration file will contain parent tag <beans> and
its child tags <bean>. This config file will contain the entries
of all the beans that we will use into the project.
We need to write the following code into the config file:
Check the availability of this file in classpath.
The <bean> tag will reflect the concern bean by property class.
The property id will act as an object to the class. Using the id
attribute we can call any method of the class. <property> tag will
represent the data member of the class with a name attribute and its
value by value attribute.
Step
5 – Running the
application
Everything is set. Now the remaining work is to run
the application. Compiling and running the application is the last step to follow
creating the source and configuration files. To the run, the application using
eclipse open SpringMainApp.java, use the Run option available in
the eclipse or press Ctrl+F11. If there is no error then the following
output will be printed.
Spring Hello World: welcome
With the end of this tutorial, our first spring application is created. To print another message change the message property in the configuration file.
This was all about how to create the “Hello World”
example of spring application. In the further chapter, we will discuss “IoC Container”
0 Comments