You are on page 1of 4

IOC containers

1.Used to read the configuration file


2.create the bean objects
3.provide the bean objects to application

Two types of IOC containers


1.Beanfactory ----lazy loader
2.ApplicationContext -----eager loading

BeanFactory :---
1.fundamental or base conatiner provided by spring framework.
2.Provide the basic functionalities to spring framework like creation
,maintenacnce of bean objects as provided in configuration file
3.To represent BeanFactory IOC container Spring framework has provided

BeanFactory(I)
org.springframework.beans.factory.Beanfactory interface

4. The implementation class of BeanFactory Interface is


org.springframework.beans.factory.xml.XmlBeanFactory

5.Steps for using the BeanFactory IOC container

1.Create Resource Object


2.create BeanFactory Object
3.Get bean oject and access the business logic

Note: XmlBeanFactory has been deprecated

ApplicationContext :---
1.AplicationContext is an extension of BeanFactory Container
2.Provides some advanced features of internationlization ,Event Hnadling,
along with the fundamental functionalities BeanFactory is providing
3.Represented by Interface org.springFramework.context.ApplicationContext
(Interface)
4.ApplicationContext is child interface of BeanFactory

ApplicationContext actions:
-->configuraaion file loading
-->configuration file parsing
-->read data from configuration file
-->Recognize all bean configurations
-->get bean name and location of all the bean classes
-->bean class loading-->create bean object -->bean initialization-->storing bean
objects in form of key-->value pairs( id name of property tag (Key)-->Class
Objects(Value)))

Beans Scope : --
In J2SEE we can define the scopes using access modifiers like
private,public,protected ,default
similarly in Spring we can define bean scopes :--
1.Singleton Scope (default )
2.prototype
3.request scope
4.session scope
5.globalSession scope
6.application scope
7.webSocket scope

#To define the Spring configuration file without XML file by using Annotations

@Configuration ----class level annotation


@Bean(name="bean" ,autowire=Autowire.byType) ------method level annotation
public BeanObject method(){
return new Bean();}

Bean Life Cycle: ----


-----------------------------------------------
#1.Bean Loading ------------IOC container will load the Bean class bytecode firat
locating in local ,then rt.jar finally from env. variables by reflection api

Class c= Class.forName("com.core.MyBean");

#2.Bean Instantiation

c.newInstance();

Bean Instantion occurs by 3 ways: --


1.By Constructor
2.Static Factory method
3.Instance factory method

#3.Bean Initialization
#4.Bean DeInstantiation

Bean Initialization and Destruction


IOC container will initialize the Beans and destroy the Beans.
Bean destruction occurs when Business logic is executed or IOC conatiner is
shutdown .
Three ways to perform Bean initialization and Bean Destruction
1.By using Custom Initialazation and Destruction methods
2.By using InitializingBean and DsiposableBean callback interfaces
3.By using @PostConstruct and @PreDestry annotations

1.By using Custom initialization and destruction Method


In this approach we have to define the custom Initialization and destruction
methods while bean initialaiztion in Config file. using the "init-method" and
"destroy-method" as bean attributes

<bean init-method="initMethod" destroy-method="destroyMethod" id="secondbean"


class="com.test.MySecondBean" factory-bean="mybeanfactory" factory-
method="getSecondBeanObject">
"

#2.By using IniitalizingBean, its an callback method ,provides follwing methodfor


Bean Initialization

public void afterPropertiesSet()throws Exception


NOTE: -- This method will be executed by IOC container after calling all the
setXXX() methods or All Setters of Bean class by ApplicationContext.

In Spring framework ,DisposableBean is callback interface ,it provides follow


ing method to execute while destruction of Beans

public void destroy();

#3. By @PostConstruct and @PreDestroy Annotations


@PostConstruct Annotation will make a method to call by IOC container while
initializaing the Bean

@PreDestroy Annotation will call that method while bean destruction

NOTE: ----
In general We can do Bean initializazation and destruction using all or one of
the above approaches ,But if we use all above three approaches in a single bean at
same time then the Order of Bean initialization wil be following

a)an Initialization method marked with @PostConstruct Annotation


b)afterPropertiesSet() method provided or implemented by InitializingBean(I)
Interface
c)an initialization method configured with "init-method" in Config File
<bean id="id" class="Bean" init-method="initMethod"/>

###Destruction Order

a)Adestruction method marked by @PreDestroy Annotation


b)destroy() method implemented by implementing the DisposableBean(I) interface
c)A destruction method defined by "destroy-method" in config file

##For calling the init and destroy methods(with same name) for all the Beans we
can mention default-init-method="initMethodNameInAllBeans" deault-destroy-
method="destroyMethodNameInAllBeans" .So while loading the IOC it will call init
destroy methods of all Beans where method is defined after setting all
properties of corresponding bean

--------------------------------------
Bean Inheritance

<bean name="shape" class="com.core.Shape" >


<property name="name" value="shapes"/>
<property name="type" value="triangle"/>
<property name="sides" value="3"/>
</bean>

<bean name="child" ###parent="shape"### class="com.core.Triangle">


<property name="corners" value="3" />
</bean>
#BeanPostProcessor
When ApplicationContext is activated , IOC container will perform some actions
automatically implicitly

1.ApplicationContext will read Bean difinitions from configuration file


2.ApplicationContext will recognize beans classes and creates objects for
beans.
3.ApplicationContext will initialization by executing initialization methods
4.When IOC container is shutdown then bean objects are destroyed .
In Above Bean lifecycle if we want to provide the customization over bean
initializations then we have to us predefined interface
org.springframework.beans.factory.config.BeanPostProcessor"

BeanPostProcessor interface contains following two methods


1.public void postProcessBeforeInitialization(Object bean , String name) throws
BeansException
--->This method will be executed before bean initialization and after bean
instantiation .. ie. before executing init() method if we provide custom
initialization method

2.public void postProcessAfterInitialization(Object bean, String name)throws


BeanException
--->it will be executed after bean initialization .ie after init() method

Nested Beans :--


Declaring a bean configuration in another bean configuration is called as Inner
Bean or Nested beans.

<beans>
<bean id="" class="parentClass">
<property name="innerbean">
<bean id="innerbean" class="Class">
</bean>
</property>
</bean>>
</beans>

You might also like