14 Dec 2017

what is lazy initialisation or Lazy-Init in spring IOC ? Let we have a spring Bean configuration file with 10 beans then how can we enable pre-instantiation only on 6 beans ?


Take 6 beans as  singleton scope beans and 4 beans as prototype scope beans and don't make prototype scope beans as dependent to singleton scope beans. and also make sure that lazy-init="true" is not placed in singleton scope Beans  configuration.
hence we know that IOC conatiner creates bean class object for every factory.getBean(-,-) method call.so even though object is created through pre-instantiation process that will be wasted because it has to create new object when factory.getBean(-,-) method call.


Lazy Initiation in Spring IOC

As we know Spring’s bean factory is pre-initiate all the beans when first time creating the factory. This is good practice because if there is any dependency error everything can be resolved at the time of start up. 
we can make our ApplicationContext container not to perform pre-instantiation of singleton Spring Bean by keeping lazy-init="true" in bean tag.
we can understand it by following example:
Employee.java
package com.ch.ioc;

public class Employee {
          private String name;

          private Address address;

          public Employee() {
                   System.out.println("Employee Constructor");
          }

          public Address getAddress() {
                   return address;
          }

          public void setAddress(Address address) {
                   this.address = address;
          }

          public String getName() {
                   return name;
          }

          public void setName(String name) {
                   this.name = name;
          }
}

Address.java
package com.ch.ioc;

public class Address {
          public Address() {
                   System.out.println("Address Constructor");
          }

          private String city;
          private String pincode;

          public String getCity() {
                   return city;
          }

          public void setCity(String city) {
                   this.city = city;
          }

          public String getPincode() {
                   return pincode;
          }

          public void setPincode(String pincode) {
                   this.pincode = pincode;
          }

}

LazyInit.java
package com.ch.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LazyInit {
          public static void main(String args[]) {
                   //Initializing context
                 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
                   System.out.println("After initialization");
                   applicationContext.getBean("addressBean");
          }
}

ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
          xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.5.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
          "employeeBean" class="com.ch.ioc.Employee" />
          "addressBean" class="jcom.ch.ioc.Address" lazy-init="true" />
</beans>