Spring Framework IOC lazy loading (Lazy-init)

1. What is lazy loaded

In Web applications, often system bottlenecks in that the response speed of the system. If the system response is slow, users will be complaining mood, and therefore the value of the system will be greatly reduced. Therefore, for faster system responsiveness, it is very important.
Web applications that do the most and back-end database interaction, query the database is a kind of time-consuming process. When too many database records, query optimization has become more important. To solve this problem, it was suggested that the concept of cache. The user data cache is frequently used in memory for quick access. After the user performs a query operation, records check will be in the cache. When the user query again, the system will first read from the cache, if the cache is not, then query the database. Caching some extent, system performance is improved, but when a large amount of data, it is not appropriate buffer. Because of the limited memory capacity, much of the data in memory, it will affect computer performance. While another technique, lazy loading can solve this problem.

2.Spring container on lazy loading:

Spring IOC container by default when initially brought us to configure all of the bean in the configuration file, but if too many bean configuration file, then this approach will make container slow start-up mode, and some bean will not be used to in a container Therefore produce concept lazy loading (lazy-init), so some of the bean initialization delay timing is initialized when calling.

3. lazy loading property settings:

We add lazy-init attribute in the bean top in the configuration file, the value of the property may be to true , false , default. The default is false , I want to use lazy loading you need to set the value to true .

4. Global lazy loading settings:

Setting bean global lazy load defaults

Configuration setting global profile rise above it. Container will not initialize all of the bean at startup, only one in use when the current bean bean will be initialized to save space.

<beans default-lazy-init="true"></beans>

Configuration setting global profile rise above it. Container will not initialize all of the bean at startup, only one in use when the current bean bean will be initialized to save space.

The lazy loading case:

eg:

 <bean id="lazyLoad" class="com.lanou3g.demo" lazy-init="true" >

(Bean is set to lazy load the properties will not be initialized when the container starts only when (getBean) calls will be initialized)

<bean id="notLazyLoad" class="com.lanou3g.demo" >

(It is not set to the bean property lazy loading, when the container is initialized start)

Guess you like

Origin blog.csdn.net/weixin_43311054/article/details/91999712