Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring bean Lazy-initialization

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 488
    Comment on it

    Spring container initialize all the beans registered in the bean configuration file at the time of context start up. When application starts first time all beans are initiated. This helps container to search dependencies are exist or not at the time of application starts-up. Spring also provides lazy-initialized beans feature.

    Following are some drawbacks of the lazy initialization :

    1. Application takes long time to start-up because BeanFactory needs to initialize all beans.
    2. Takes more memory to hold the bean objects.

    So Spring Framework provides the feature to explicitly on or off the lazy initialization. Spring has attribute called lazy-init which inform the SpringIOC container that need to create a bean at the time of start up or not. There are two scenarios :

    1. lazy-init = true (When request is received bean will initialized)
    2. lazy-init = false (At the time of spring container initialization Bean will initialized )

    Example of lazy-init in Spring:

    Person.java

    package com.evon.example.lazyInitializeBean;
    public class Person {
        private int id;
        private String Name;
        private Address address;
    
        Person(){
            System.out.println("Person class is initialized.");
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return Name;
        }
        public void setName(String name) {
            Name = name;
        }
        public Address getAddress() {
            return address;
        }
        public void setAddress(Address address) {
            this.address = address;
        }   
    }
    

    Address.java

    package com.evon.example.lazyInitializeBean;
    public class Address {
        private String city;
        private String country;
    
        Address(){
            System.out.println("Address class initialization.");
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        }
        public String getCountry() {
            return country;
        }
        public void setCountry(String country) {
            this.country = country;
        }
    }
    

    bean.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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
      <bean id="personBean" class="com.evon.example.lazyInitializeBean.Person" lazy-init="true"/>
      <bean id="addressBean" class="com.evon.example.lazyInitializeBean.Address"/>  
     </beans>
    

    Main.java

    package com.evon.example.lazyInitializeBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
        public static void main(String[] args) {
            System.out.println("Container Starting...");
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/lazyInitializeBean/bean.xml");
            System.out.println("Container Started...");
            Person person = (Person) context.getBean("personBean");
        }
    }
    

    Output:

    Container Starting...
    Jan 20, 2015 12:01:08 PM  [/home/sumit/findnerd/blogs/CoreExamples/src/main/java/com/evon/example/lazyInitializeBean/bean.xml]
    Jan 20, 2015 12:01:08 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@d92bd2: defining beans [personBean,addressBean]; root of factory hierarchy
    This is Address initialization.
    Container Started...
    This is Person initialization.
    

    We can see in above example, the Person bean is configured as lazy-init=true. So when program runs, the Address bean is initialized at the time of container start up, but Person bean is initialized at the time of user requested for the bean using getBean() method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: