In general all bean are initialized at startup. To stop such unnecessary initialization we use lazy initialization which create the bean instance when it if first requested
The lazy-init attribute are defined in the xml file at the <bean/> element.
Address.java
package lazyinitialized.bean.example;
|
Student.java
package lazyinitialized.bean.example;
|
LazyInitMain.java
|
package lazyinitialized.bean.example; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LazyInitMain { public static void main(String args[]) { BeanFactory beanfactory = new ClassPathXmlApplicationContext( "context.xml"); System.out.println("Initialization done when require"); beanfactory.getBean("addressBean"); } } |
context.java
<?xml version="1.0" encoding="UTF-8"?>
|
| Inside Student Constructor Initialization done when require Inside Address Constructor |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.