Spring Bean Configuration with XML

The creating POJO class or Java Bean class object is created by the developers or programmers. Whenever any need for the POJO class or Java Bean class in the project we have to create an object of that particular class manually. But the Spring Framework provides us with the Spring Bean class concept. For the Spring Bean class objection creation and managed by Spring Container we don’t need to worry about creating an Object Spring Bean class. We need to configure the POJO class or Java Bean class in the XML configuration file. So, the container will be treated as the Spring Bean class.

What is Spring Bean?
   • The java class whose object is created and managed by the Spring container is called Spring Bean.
   • Spring Bean can be the POJO class or java bean class but its object must be created and managed by the Spring IOC(Inversion of control) container.
   • To make a java POJO class as Spring Bean it must be configured in the spring bean configuration file (XML) to take a spring container to create and manage that spring bean class object.
   • Generally, we can’t take an abstract class or interface like Spring Bean because we can’t create an object for an abstract class and interface.
   • Spring Bean can be a user-defined POJO class or predefined class or third-party supplied class.

What is Spring Container?
Spring Container is the main core component in the Spring Framework. Because the Spring container is responsible for the creation of objects, configuring the objects, wiring the objects, and managing their life cycle from creation to destruction. Spring Container used Dependency Injection(DI) to manage the components for making an application. These objects known as the Spring Beans are managed and created by the Spring Container.
Spring Container gets its command of which object to instantiate, configure, and wiring by reading the configuration metadata given. The Configuration metadata represents XML-based, Java Code-based, or Annotation based. The Spring IoC(Inversion of Container) container uses Java POJO classes and configuration metadata (XML configuration file) for making an application.

How many IoC(Inversion of Control)containers?
The Spring Core module is providing two IoC(Inversion of Control) containers or Spring Containers are:
      • Spring BeanFactory Container
      • Spring ApplicationContext Container

Spring BeanFactory Container: This is the basic container that performs Spring Bean management and dependency injection.

Spring ApplicationContext Container: It is an advanced container that also uses the BeanFactory container. It is very easy to add Spring AOP module features like message resource handling etc.

In this example of the project, we will create a POJO class i.e., CricketCoach.java, XML configuration file i.e., applicationContext.xml, and the main class i.e., ClientApp.java.In the XML configuration file, we will configure our Cricket.java POJO class as Spring Bean because whenever Spring Container read that XML configuration file then the Spring Container configures the POJO class to the Spring Bean class. In the main class, we will activate the Spring container and load the XML configuration file so that Spring Container configures the Spring Bean class then we will get the Spring Bean class from the Spring Container.

How to create Spring Bean Configuration with XML file?
In this Project, we will configure simple a POJO class as a Spring Bean class so we can understand it easily. In the POJO class, we will create some method that returns a String message to print as an output. So we can analyze in this way we will configure Spring Bean with XML configuration. 

Development Process:
1. Keep eclipse IDE ready
2. Project Structure
3. Create a POJO Class
4. Configure your Spring Bean
5. Add the jar file to the Build Path of the Project for the Spring Bean Configuration with an XML example
6. Run the App

1. Keep eclipse IDE ready
2. Project Structure


3. Create a POJO Class
CricketCoach.java (POJO Class):

package com.poc.bean;
public class CricketCoach {
public String getDailyWorkout(){
return "4 hours the Batting Practice";

4. Configure your Spring Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

<!--Define your beans here-->  
<bean id="cricketCoach" class="com.poc.bean.CricketCoach" />

→ We can take any <file-name>.xml as a Spring configuration file. But it is recommended to take the name  “applicationContext.xml” file in standalone spring applications.
→ <bean id=”cricketCoach” class=”com.poc.bean.CricketCoach” /> the bean id name is used to retrieve a bean from the Spring container.

5. Add the jar file to the Build Path of the Project for the Spring Bean Configuration with an XML example
Right Click on the  Project(Spring_Bean_Config_With_XML) ⇒ Build Path ⇒ Java Build Path ⇒ libraries ⇒ Add External jars



package com.poc.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.poc.bean.CricketCoach;

public class ClientApp {
public static void main(String[] args) {
//activate the IOC container and load the spring Configuration file
ClassPathXmlApplicationContext context=new 
//retrieve bean from spring container
CricketCoach teamCoach=context.getBean("cricketCoach",CricketCoach.class);
// call the method on bean

6. Run the App

4 hours the Batting Practice

This example is explained What is Spring Bean? What is Spring Container? How many IoC(Inversion of Control) containers? How to create Spring Bean Configuration with XML file? 

Leave a Comment