Introduction

  1. The central component of a Spring application is the containers.
  2. Spring containers manage the life cycle of the bean in the applications,and are responsible for wiring of these beans.
  3. Spring provides two interfaces that act as containers,namely BeanFactory and ApplicationContext

BeanFactory

  1. BeanFactory is an interface and available in org.springframework.beans.factory package.
  2. BeanFactory container is the root container that loads all the beans and provides dependency injection.
  3. BeanFactory is the basic container.

Loading the configuration file using BeanFactory

BeanFactory factory=new XmlBeanFactory(new ClassPathResource("beans.xml"));

Example:

  1. Create the java project.
  2. Go to configure build path option
  3. Add the spring jar files to the project using build.
  4. Create a pojo class named Message.java
  5. Create the Spring bean configuration file and add Message class as bean entry.
  6. Create client program and run it.
  7. Final project structure

Implementation

  1. Create the java project.
  2. Creating Java Project

  3. Configuring build path
  4. ConfigureBuildPath

  5. Add the spring jar files to the project using build.
  6. SelectionOfJarfiles

  7. Create a pojo class named Message.java
    package com.java2learn.core;
    
    public class Message {
    
    	private String message;
    
    	public void setMessage(String message) {
    		this.message = message;
    	}
    
    	public String getMessage() {
    		return message;
    	}
    }
    
    
  8. Create the Spring bean configuration file and add Message class as bean entry.
    <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="message" class="com.java2learn.core.Message">
        <property name="message" value="Hello World"></property>
        </bean>
    	 
    </beans>
    
  9. Create client program and run it.
    package com.java2learn.client;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    import com.java2learn.core.Message;
    
    public class Client {
    
    	public static void main(String[] args) {
    	     
            BeanFactory factory=new XmlBeanFactory(new ClassPathResource("Spring-Core.xml"));
            Message message=(Message)factory.getBean("message");
            System.out.println("Message Name::"+message.getMessage());
             
        }
    
    }
    
    
  10. Final Project structure.
  11. FinalProjectStructure

Output:

Message Name::Hello World

Download Project(With Lib) : SpringCore

Note:BeanFactory is a lazy container,which means that it instantiate the bean and configures it only when the getBean() method is called.

ApplicationContext

  1. ApplicationContext is an interface and available in org.springframework.contexty package.
  2. ApplicationContext is the sub interface of BeanFactory.
  3. ApplicationContext used as a container in the enterprise applications with a number of features.

Loading the configuration file using ApplicationContext container.

Use the above example for the ApplicationContext container demo except client program.

Example:

package com.java2learn.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2learn.core.Message;

public class ApplicationContextDemo {

	public static void main(String[] args) {
	
		//loading the spring configuration file
		System.out.println("before loading");
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("Spring-Core.xml");
		System.out.println("after loading");
		//getting the bean instance which is defined in the configuration file.
		Message message=(Message)applicationContext.getBean("message");
		//calling getMessage() method to display the message.
		System.out.println(message.getMessage());
	}
}

Output:

before loading
Message object is creating..
after loading
Hello World

Note:ApplicationContext container instantiate all the beans while loading the configuration file.

Loading Spring configuration file using Maven plugin