"Welcome To Ashok IT"
"Spring Boot and MicroServices"
Topic : ApplicationContext Container
Date : 29/08/2024
(Session - 13)
_____________________________________________________________________________________________________________________________
Yesterday Session
=================
* Setter injection Vs Construction Injection
* Circular Dependency Problem
Today Session
=============
ApplicationContext
==================
* In Spring Framework, the ApplicationContext interface represents the Spring IoC (Inversion of Control) container and provides advanced capabilities for managing and configuring Spring beans. It serves as the central interface for interacting with the container, enabling you to load, access, and manage beans within your application.
* The ApplicationContext container is responsible for performing the following tasks:
1) Bean instantiation: The container creates and manages instances of beans defined within your application. It handles the lifecycle of beans, including their creation, initialization, and destruction.
2) Dependency injection: The container injects dependencies into beans, resolving their dependencies and managing their relationships. This promotes loose coupling between components and makes the application more maintainable and testable.
3) Configuration management: The container reads and interprets bean definitions and other configuration metadata, such as XML files, Java annotations, or Java-based configuration classes. It uses this information to wire beans together and apply additional settings.
4) AOP (Aspect-Oriented Programming) support: The container supports the use of AOP to separate cross-cutting concerns from the business logic of your application. It allows you to apply aspects to beans, enabling functionalities such as logging, transaction management, and security.
5) Event publishing: The container provides the capability to publish and handle events within the application. Beans can publish events, and other beans can listen for and respond to those events.
* Spring provides several implementations of the ApplicationContext interface, each catering to different application environments and requirements. Some common implementations include:
1) ClassPathXmlApplicationContext: Loads the configuration metadata from XML files located in the classpath.
2) FileSystemXmlApplicationContext: Loads the configuration metadata from XML files in the file system.
3) AnnotationConfigApplicationContext: Uses Java annotations to define the configuration classes.
4) WebApplicationContext: Specialized implementation for web applications, supporting additional features like handling HTTP requests and responses.
* To use the ApplicationContext container, you typically create an instance of the appropriate implementation class and configure it with the necessary information about your application's beans and dependencies. Then, you can retrieve beans from the container and use them within your application.
ApplicationContext Container Application
=========================================
Student.java
============
package com.ashokit.beans;
public class Student {
private String id;
private String name;
private String course;
private String emailId;
public Student() {
System.out.println("Student Class Constructor");
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setCourse(String course) {
this.course = course;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return String.format("Student Information [StudentId %s and StudentName %s and course %s and emailId %s", id,name,course,emailId);
}
}
Test.java
=========
package com.ashokit.beans;
public class Test {
public Test() {
System.out.println("Test Class Constructor.......");
}
}
spring.xml
==========
SpringClient.java
=================
package com.ashokit.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringClient {
public static void main(String[] args) {
/*System.out.println("Started ::: BeanFactory Container is Lazy");
//creating the BeanFactory Container
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
//XmlBeanDefinitions
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions("spring.xml");
//Requesting the bean object
Student student = factory.getBean(Student.class);
System.out.println(student);
System.out.println("Ended ::: BeanFactory Container is Lazy");*/
//Creating the ApplicationContext Container
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println("Started ::::: ApplicationContext Container");
//Requesting the Spring beans from container
Student st = context.getBean(Student.class);
System.out.println(st);
Test test = context.getBean(Test.class);
System.out.println("Test ::::" + test);
System.out.println("Ended ::::: ApplicationContext Container");
}
}
OUTPUT
=====
Started ::::: ApplicationContext Container
Student Class Constructor
Student Information [StudentId AIT123 and StudentName Mahesh and course SpringBoot And Microservices and emailId mahesh.ashokit@gmail.com
Test Class Constructor.......
Test ::::com.ashokit.beans.Test@3e96bacf
Ended ::::: ApplicationContext Container
BeanFactory Vs ApplicationContext
=================================
BeanFactory
===========
1. BeanFactory container provides limited no of services i.e.,Managing Spring Bean life cycle,DI.
2. In BeanFactory container will works with XML files, Annotations
3. BeanFactory container will do bean instantiaion as "Lazy".
ApplicationContext
===================
1. ApplicationContext container provides no of services i.e.,Managing Spring Bean life cycle,DI,EventHandling,AOP.
2. ApplicationContext container will work with Xml Files,Annotations,JavaBased Configuration.
3. ApplicationContexdt container will do bean instatiation as "Early".
NOTE
====
* lazy-init=true to mark the Spring Bean object as lazy behaviour means until Programmer gives Request by using getBean() ApplicationContext container will not create the Spring Bean Object.
Spring Bean scopes
==================
* In Spring framework by default every spring bean as "Singleton".
* As Programmer we can't write the singleton logic because spring container will take care of singleton behaviour of Spring beans.
* Singleton means only one object for certain Java class and will be returning same object for every request.
* In order to define the scope of Spring bean in xml approach we can use attribute i.e., scope of tag
>>> XML Approach
* We can have another values of scope attribute in Spring Framework
scope ="singleton/prototype/request/session/global-session/application";
singleton/prototype >>> Applicable for any type of spring application(Standalone,Web,Distributed);
request/session/application >> Applicable for Web Applications in spring Framework
global-session >> Portlet Programming i.e.,Liferay development
websocket >> Reactive Programming and this is from spring 5.x version
application is newly added scope from spring 5.x version
* We can use @Scope Annotation in Spring Framework to define scope of Spring bean
@Scope is class level (or) method level annotation
In Java based configuration we can use @scope annotation at method level