【Spring】07 Lazy loading

1.Definition

Lazy Initialization is a powerful feature in the Spring framework. It allows us to postpone the initialization of Bean until it First time requested. This approach is very beneficial for improving application performance and reducing resource consumption.

Insert image description here

2. Function

By default, the Spring container initializes all beans when the application starts. However, in some cases, there may be a large number of beans, and not every bean is used immediately when the application starts. This is where lazy loading comes into play!

The main functions of lazy loading include:

  • Reduce startup time

    Only initializing a bean when it is needed can significantly reduce application startup time

  • Reduce resource consumption

    If some beans are rarely used, setting them to lazy loading can reduce the usage of system resources.

3. Configuration method

In Spring, configuring lazy loading of beans is very simple. This can be achieved through XML configuration, Java configuration or using annotations

1) XML configuration

Used lazy-init="true" configured lazy loading

<bean id="springDemo" class="org.example.cheney.SpringDemo" lazy-init="true">
    <!-- Bean的其他配置 -->
</bean>

2) Java configuration
@Configuration
public class AppConfig {
    
    

    @Bean
    @Lazy
    public SpringDemo springDemo() {
    
    
        return new SpringDemo();
    }
}
3) Annotation method
@Component
@Lazy
public class SpringDemo {
    
    
    // Bean的配置
}

4. Application scenarios

Lazy loading usually applies to the following scenarios:

  • Large Bean Collection

    When there are a large number of bean definitions, but only a few are used when the application starts, you can use lazy loading to reduce startup time.

  • Resource intensive operations

    If the initialization process of a certain bean is very time-consuming or resource-intensive and is not used every time, consider lazy loading.

  • Conditional Bean Loading

    When the loading of beans depends on certain conditions, and these conditions cannot be determined when the application starts, lazy loading is a good choice

5. Precautions

When using lazy loading, you need to pay attention to the following points:

  • AOP proxy issues:

    If a lazy-loaded bean uses an AOP proxy, lazy loading will be disabled and the bean will be initialized when the application starts. This is because the AOP proxy needs to be generated when the bean is initialized.

  • Singleton mode:

    By default, lazy-loaded beans are singletons. If you need to create a new bean instance each time, consider using prototype scope.

  • Release resources promptly:

    Although lazy loading can reduce resource usage, you still need to be careful to release the resources that may be occupied in a timely manner before the bean is actually used to avoid unnecessary overhead.

Summarize

Lazy loading (Lazy Initialization) is a useful feature in the Spring framework that can significantly improve application startup performance and reduce resource consumption. Through simple configuration, we can decide which beans need to be initialized immediately based on actual needs, and which beans can be postponed until they are actually needed. In large applications or resource-intensive operations, lazy loading is a powerful tool that helps optimize system performance.

Guess you like

Origin blog.csdn.net/yanyc0411/article/details/135027230