Search This Blog

Friday 30 August 2013

Spring bean scopes and Spring bean life cycle

Spring Bean scopes


There are six bean scopes in spring. Two scopes are related with normal beans, these are singleton and prototype. Other three related with web-aware beans, these are request, session global session.
Singleton
Spring container creates single object instance for a bean definition.
Prototype
Spring container creates any number of object instances for a bean definition.
Request
Spring container creates bean object instance for a bean definition for each HTTP request. This scope is valid in the web-aware spring context.
Session
Spring container creates one bean object instance for a bean definition for the lifetime of a single HTTP Session. This scope is valid in the web-aware spring context.
Global session
A global Session that is shared amongst all of the various portlets that make up a single portlet web application.
Spring container creates one bean object instance for a bean definition for the lifetime of a single HTTP Session. This scope is valid in the web-aware spring context.



Spring bean life cycle
Bean Instantiation – Instantiating  the bean
Set Bean Name – by using setBeanName method
Populate properties – Injecting  bean  properties
Set Bean Factory – by using setBeanFactory method, if bean implements BeanFactoryAware
Bean Pre Initialization - by postProcesserBeforeInitialization method, if bean implements
                                     BeanFactoryAware
Bean Initalization – by using init method
Bean Post initialization – by using postProcessAfterinitalization method, if bean 
                                          implements BeanFactoryAware
Bean Ready to Use - Now the bean is ready to use
Destroy bean – by using destroy method

Sample Code:

//XML declaration

<?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-3.0.xsd"
    default-init-method="dInitMethod"
    default-destroy-method="dDestroymethod">
      <bean id="springBean" 
          class="SpringLifeCycleMehodBean" />
</beans>

-------------------------------------------------------------
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;

public class SpringBean implements BeanFactoryAware, BeanNameAware,
{

//Member variables declaration
@Override
public void setBeanName(String arg0) {
System.out.println("Method setBeanName...");
}

@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("Method setBeanFactory...");
}

@PostConstruct
public void postConstruct() 
{
System.out.println("Method postConstruct...");
}

@PreDestroy
public void preDestroy() 
{
System.out.println("Method pre Destroy...");
}

}


 

No comments:

Post a Comment