In Spring, ApplicationContext creates and configures all beans defined as singletons when container initialization by default. This helps to immediately spot any configuration errors or dependency issues when the application starts. However, in some cases, you may want to delay initialization of beans until they are actually needed. This can be achieved by marking the Bean definition as lazy initialization.
Delay initialization is also called lazy initialization, which means not initializing a bean in advance, but only when it is actually used.
A lately initialized bean is usually initialized on the first use; or is initialized as the bean is initialized when it is injected as a dependency object by a non-delay initialized bean, because the delayed initialization bean is used at this time.
Container Management Initialization Bean eliminates programming and implements delayed initialization. It is completely controlled by the container. It only needs to be configured on the bean definition that requires delayed initialization. It is simpler than programming and is intrusive.
In Spring, you can set the bean to delay initialization in the following ways:
Using XML configuration: In an XML configuration file, you can use it to<bean>
Set the lazy-init attribute to true on the element to specify that the bean should be delayed init. For example:
<bean class="" lazy-init="true"/>
Using Java configuration: In Java configuration, you can use @Lazy annotation to specify delayed initialization on the Bean's fields, methods, or constructor parameters using @Bean methods. For example:
@Configuration public class AppConfig { @Bean @Lazy public MyBean myBean() { return new MyBean(); } // Or delay initialization during injection @Autowired @Lazy private MyBean myBean; }
Note that in Java configuration, the @Lazy annotation can be applied to fields, methods, or constructor parameters to delay injection of beans. When applied to the @Bean method, it affects the initialization of the bean defined by the method.
Global settings: In XML configuration, you can set it<beans>
The default-lazy-init attribute of the element is true to specify that all beans should be delayed init. But please note that this only affects beans defined by XML.
<beans default-lazy-init="true"> <!-- Bean definitions go here --> </beans>
In Java configuration, there is no direct global setup, but you can achieve similar effects by adding @Lazy annotation to all beans or using other programming methods.
When a bean is marked as delayed initialization, the Spring IoC container does not create an instance of the bean immediately upon startup. Instead, it creates and configures an instance of the bean when the bean is first requested (for example, through the getBean() method or auto-assembly). This helps reduce overhead at application startup and allows for finer granular control of the life cycle of the bean.
This is the end of this article about the implementation example of Spring Delay Bean Initialization. For more related spring Delay Initialization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!