Building First Spring application

    1 Votes

Now, let us create our first spring application. Here, we had provided detailed step by step process to create and run our first Spring application with the help of detailed explanation.

Create a folder called "configFiles" under "SpringAssignment/src" folder.

spring source folder

Create a file named beans.xml in configFiles
Add following lines into 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-3.0.xsd">
<bean id="helloWorld" class="com.example.hello.HelloWorld">
<property name="greet" value="Hello World! Welcome to Spring"/>
</bean>
</beans>

spring bean

Create a package called com.example.hello under "SpringAssignment\src"

Create a java file HelloWorld.java under the package "com.example.hello"

Write following in HelloWorld

package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {

private String greet;
public String getGreet() {
return greet;
}
public void setGreet(String greet) {
this.greet = greet;
}

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("configFiles/beans.xml");

HelloWorld bean =(HelloWorld) ctx.getBean("helloWorld");
System.out.println(bean.getGreet());
}
}

spring hello world example

Run as java application output will be

Hello World! Welcome to Spring

spring hello world output

Understanding the Application

Before understanding the application code, we try to understand How spring works?

  • When you start an application by invoking java main, Spring creates a Container and try to search Spring configuration file which is “beans.xml” in this case. Spring configuration file does not have a predefined name, you can name whatever you wish but you have to pass the Path. 
  • From the given path container, spring load the XML file.
  • Container parses the XML files and reads the Spring bean register from that file.
  • Now Container tries to instantiate those beans using java reflection using the fully qualified class name given in configuration file.
  • Then Bean life cycle phases are start. We will discuss it later.
  • Container tries to wire beans among themselves. The Dependency injection is done here.
  • Spring container register beans information in the internal map where bean id is the Key of the map.
  • Now Beans are ready to use.

So, Spring Configuration files play a pivot role in Spring application, Spring container reads all instructions from that file. So if anything goes wrong in that file (say forgot to close a tag) or File path is wrong spring application will not load and it throws an exception.

TIP: Always pay close attention to configuration file

Diagram:

spring configuration file

Now, we are in a position to understand code,

beans.xml:

Here we register our java class by bean tag. Please note we put a property id in bean tag. This is a unique id. No two beans should have the same Id, else Spring container will complain and throw an exception. Id is an important property and always provides a unique id for each bean. Later using this id, we can get an instance of Spring bean from Spring container.

Next, we have written a property tag under bean tag where name attribute must be same as java attribute name (POJO attribute). Then we pass a value which will actually inject in the HelloWorld bean.

In the main method, we spawn the Spring container by the following statement

ApplicationContext ctx = new ClassPathXmlApplicationContext("configFiles/beans.xml");

We use ClassPathApplicationContext which will search for configuration file in the classpath. If it is not found, spring will throw an exception.

In next line, we try to get the HelloWorld bean by id, which was stated in the configuration file.

After that, we invoke getGreet() method which shows the value which was passed from the configuration file.

Here, Internally Spring injects the value into the java(POJO) property via a Setter method. So you have to provide a setter method in HelloWorld, else Spring can’t inject the value and eventually throw an exception.

Related Articles

Spring Framework - Overview
Spring Architecture
Step by step guide for Spring configuration
Spring Bean XML File
Spring Container
Spring Bean - Learn with an Example
Spring Bean Scope
Spring Bean Life Cycle
Spring Bean Post Processor
Spring Inner Bean
Spring Autowiring
Spring Autowiring Types
Spring Qualifier with Autowiring
Spring Collection Injection
Spring Inheritance
Spring Event Handling
Dependency Injection
Spring AOP
Spring AOP Example

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know