The Initializer extension point

This special case of a bean post-processor causes init (@PostConstruct) methods to be called. Internally, Spring uses several BeanPostProcessors (BPPs) CommonAnnotationBeanPostProcessor to enable initialization. The following diagram illustrates the relationship between initializer and BPPs.

Now let's see the following example for the Initializer extension point in XML:

Namespace <context:annotation-config/> explicitly enables many post-processor, let see the following configuration file in XML:

    <?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"
init-method="populateCache"/> </beans>

In the preceding configuration code, you can see that I have defined some beans out of which one of the bean accountRepository repository has the init method attribute of the bean tag; this attribute has a value, populateCache. This is nothing but an initializer method of the accountRepository bean. It is called by the container at the time of bean initialization if the post-processor is explicitly enabled by the <context:annotation-config/> namespace. Let's see the JdbcAccountRepository class, shown as follows:

    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 populateCache(){ System.out.println("Called populateCache() method"); } }

In the Java configuration, we can use initMethod attribute of the @Bean annotation as follows:

    @Bean(initMethod = "populateCache") 
    public AccountRepository accountRepository(){ 
      return new JdbcAccountRepository(); 
    }

In the Annotation-based configuration, we can use the JSR-250 annotation, @PostConstruct as follows:

    @PostConstruct 
    void populateCache(){ 
      System.out.println("Called populateCache() method"); 
    } 

We have seen the first phase of a bean life cycle, where Spring loads the bean definitions by using XML-, Java-, and Annotation-based configuration, and after that, the Spring container initializes each bean in the correct order in the Spring application. The next diagram gives an overview of the first phase of the configuration life cycle:

The last diagram shows Spring bean metadata in any style-XML, Annotation, or Java-loaded by the respective implementation of ApplicationContext. All XML files are parsed, and loaded with the bean definitions. In Annotation configuration, Spring scans all the components, and loads the bean definitions. In the Java configuration, Spring reads all the @Bean methods to load the bean definitions. After loading the bean definitions from all styles of configurations, BeanFactoryPostProcessor comes into the picture to modify the definition of some beans, and then the container instantiates the beans. Finally, BeanPostProcessor works on the beans, and it can modify and change the bean object. This is the initialization phase. Now let's see the next Use phase of a bean in its life cycle.

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

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