To understand the flow of Spring-based applications with ease, we will take an example of standalone application containers: ClassPathXmlApplicationContext, or FileSystemXmlApplicationContext. The whole process of dealing with Spring comprises the following three steps:
- Defining POJOs
- Creating application context (XML) files with configuration metadata
- Initializing the container
Defining POJOs: As we have seen in previous sections of this chapter, Spring considers each object in your application as a POJO. So, first you need to define POJOs. We will use simple examples to understand the concepts as per the following snippet:
package com.packet.spring.contaner.check;
public class Car{
public void showType(){
System.out.println("This is patrol car..");
}
}
Providing application context (XML) files: Create one XML file and name it application-context.xml. For the sake of simplicity, we use XML-based configuration metadata. We will see another two ways (annotation-based and Java code-based) of setting configuration metadata in upcoming sections.
Define <bean> for each of your module classes along with their configuration metadata in the application context file (application-context.xml), as per the following snippet:
<?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.xsd">
<!-- All your bean and its configuration metadata goes here -->
<bean id="myCar" class="com.packet.spring.contaner.check.Car">
</bean>
</beans>
We have defined <bean> for our POJO -Car with id="myCar". The Spring container uses this ID to get the object of the Car bean.
Initializing container: In case of a web-based application, the container (WebXmlApplicationContext) will be initialized by a web listener when an application is loaded into the servlet container. In case of a standalone application, you need to initialize the containers (ClassPathXmlApplicationContext or FileSystemXmlApplicationContext) with Java code, as per the following snippet:
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
ClassPathXmlApplicationContext and FileSystemXmlApplicationContext take resource String as an input parameter in the constructor. This resource string represents the application context (XML) file from the classpath (in the preceding snippet), or from the local filesystem (in case of the FileSystemXmlApplicationContext container).
Soon after the Spring container is loaded into memory, it processes the application context (XML) file and creates the objects for corresponding <bean> definition. You can get the instance of your bean with the help of a container, as per the following snippet:
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
// retrieve configured instance
Car carObj = context.getBean("myCar");
// use configured instance
carObj.showType();
When you call the getBean method, the container internally calls its constructor to create the object, which is equivalent to calling the new() operator. This is how Spring's IoC container creates, maintains, and assembles the objects corresponding to each <bean> definition in Spring's application context(XML) file.
When a container creates the object corresponding to the <bean> definition, you do not need to implement any specific interface, or extend any class or code in a specific way. Simply specifying the class attribute of <bean> is suffice. Spring is capable enough to create an object of any type.