Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

Spring Bean Life Cycle
Previous Home Next

Spring bean factory is responsible for managing the life cycle of beans created through spring container. The most important feature of Spring is the bean based approach. The Spring bean is created, managed and dispensed by the Spring IoC container. Each Spring bean has a lifecycle and understanding the spring bean lifecycle enables better coding.

The life cycle of a Spring bean is very easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. following are the stages in a beanís lifecycle.

  1. Instantiate: The Spring container instantiates the bean.
  2. Populate properties: Spring IoC container injects the beanís properties.
  3. Set Bean Name: Spring container sets the bean name. If the bean implements BeanNameAware, spring container passes the beanís id to setBeanName() method.
  4. Set Bean Factory: If the bean implements BeanFactoryAware, Spring container passes theBeanFactory to setBeanFactory().
  5. Pre Initialization: This stage is also called the bean postprocess . If there are anyBeanPostProcessors, theSpring container calls the postProcesserBeforeInitialization () method.
  6. Initialize beans: If the bean implements IntializingBean,its afterPropertySet()method is called. If the bean has init method declaration, the specified initialization method is called.
  7. Post Initialization: IfBeanPostProcessors is implemented by the bean, the Spring container calls their postProcessAfterinitalization() method.
  8. Ready to Use: Now the bean is ready to be used by the application.
  9. Destroy: The bean is destroyed during this stage. If the bean implements DisposableBean, the Spring IoC container will call the destroy() method . If a custom destroy () method is defined, the container calls the specified method.
InitializingBean and DisposableBean callback interfaces

Initialization callbacks:

The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies a single method:

  1. Single method
  2.  

    void afterPropertiesSet() throws Exception;
    

    So you can simply implement above interface and initialization work can be done inside afterPropertiesSet() method as follows:

  3. afterPropertiesSet() method
  4. public class Bean implements InitializingBean { 
    public void afterPropertiesSet() { 
    // perform some initialization work 
    } } 
    

    When using the XML-based configuration metadata, you can use the init-method attribute to specify the name of the method that has a return void, no argument method signature. The following is example.

  5. Example
  6. <bean id="Bean" 
    class="com.r4r.Test.Bean" init-method="init"/>
    
  7. The Test class definition
  8. public class Bean { 
    public void init() { 
    // do some initialization work 
    } } 
    

    Destruction callbacks

    The org.springframework.beans.factory. DisposableBean interface specifies a single method:

  9. Single method
  10. void destroy() throws Exception;
    

    So to enable a destroy callback, simply implement the above interface and finalization work can be done inside destroy() method as follows:

  11. destroy()
  12. public class Bean implements DisposableBean { 
    public void destroy() { 
    // do the work to be performed before bean destruction
    } } 
    

    When using the XML-based configuration metadata, you can use the destroy-method attribute to specify the name of the method that has a return void, no argument method signature. The following is example.

  13. Example
  14. <bean id="sampleBean" 
    class="com.r4r.Test.Bean" destroy-method="destroy"/>
    
  15. The class definition
  16. public class Test { 
    public void destroy() { 
    // do some destruction work 
    } } 
    
    Previous Home Next

    Tolal:0 Click:

    Show All Comments

    Did not find what you were looking for leave your name and message. We will revert within 24 hours
    Name:
    eMail:
    Comment / Feedback: