The destruction phase of the beans

In this phase, Spring releases any system resource acquired by the application services. These are eligible for garbage collection. When you close an application context, the destruction phase completes. Let's see the following lines of code in this phase:

    //Any implementation of application context 
 
    ConfigurableApplicationContext applicationContext = new   
AnnotationConfigApplicationContext(AppConfig.class); // Destroy the application by closing application context. applicationContext.close();

In the preceding code, what do you think happens when we call the applicationContext.close() method in this phase? The process that takes place is given as follows :

  • Any bean implementing the org.springframework.beans.factory.DisposableBean interface gets a callback from the container when it is destroyed. The DisposableBean interface specifies a single method:
        void destroy() throws Exception; 
  • The bean instances are destroyed if instructed to call their destroy methods. Beans must have a destroy method defined, that is, a no-arg method returning void.
  • The context then destroys itself, and this context is not usable again.
  • Only GC actually destroys objects and remember, it is called only when the ApplicationContext/JVM exit normally. It is not called for prototype beans.

Let's see how to implement it with the XML Configuration:

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:c="http://www.springframework.org/schema/c" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
4.3.xsd"> <context:annotation-config/> <bean id="transferService"
class="com.packt.patterninspring.chapter5.
bankapp.service.TransferService"/> <bean id="accountRepository"
class="com.packt.patterninspring.chapter5.
bankapp.repository.JdbcAccountRepository"
destroy-method="clearCache"/> </beans>

In the configuration, the accountRepository bean has a destroy method named clearCache:

    package com.packt.patterninspring.chapter5.bankapp.repository; 
    import com.packt.patterninspring.chapter5.bankapp.model.Account; 
    import com.packt.patterninspring.chapter5.bankapp.model.Amount; 
    import com.packt.patterninspring.chapter5.bankapp.
repository.AccountRepository; public class JdbcAccountRepository implements AccountRepository { @Override public Account findByAccountId(Long accountId) { return new Account(accountId, "Arnav Rajput", new
Amount(3000.0)); } void clearCache(){ System.out.println("Called clearCache() method"); } }

Let's see the same configuration with Java. In the Java configuration, we can use the destroyMethod attribute of the @Bean annotation as follows:

    @Bean (destroyMethod="clearCache") 
    public AccountRepository accountRepository() { 
      return new JdbcAccountRepository(); 
    } 

We can do the same using Annotations. Annotations require annotation-config or the component scanner to be activated by using <context:component-scan ... />, as seen in the following:

    public class JdbcAccountRepository { 
      @PreDestroy 
      void clearCache() { 
        // close files, connections... 
        // remove external resources... 
      } 
    } 

You have now seen the Spring bean life cycle in all its phases. In the initialization phase, Bean Post Processors for initialization and proxies. In the Use phase, Spring beans use the magic of proxy. Finally, in the destruction phase, it allows the application to terminate cleanly.

Now that you have seen the bean life cycle, let's learn about bean scopes, and how to create custom bean scopes in the Spring container.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset