美文网首页
SpringBoot:使用 @Lazy 注解懒加载

SpringBoot:使用 @Lazy 注解懒加载

作者: Rollover | 来源:发表于2020-03-20 19:02 被阅读0次

为什么需要懒加载?

我们知道,在 SpringBoot 应用程序启动的时候,会实例化一些对象加入到 IOC 容器里边,这个过程是非常耗时的,那我们想要减少这个耗时的过程就需要 @Lazy 注解

对象加入容器的时机

如下代码

package com.startdusk.forgot.service;

import org.springframework.stereotype.Component;

@Service
public class LazyService {
    public LazyService() {
        System.out.println("LazyService add to ioc container");
    }

    public void print() {
        System.out.println("lazy");
    }
}

由于我们在 LazyService 中打上了 @Service 注解,那么当程序启动的时候,LazyService 就会被立即实例化,我们在 LazyService 写了个无参的构造函数来测试,启动程序,会打印出


那么说明,在打上了 @Service 注解后,在程序启动的时候就立即实例化对象加入到容器

使用 @Lazy 延迟加入容器

那我们来改写下代码,延迟加入容器:

package com.startdusk.forgot.service;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
@Lazy
public class LazyService {
    public LazyService() {
        System.out.println("LazyService add to ioc container");
    }

    public void print() {
        System.out.println("lazy");
    }
}

再来运行一下程序:


看到运行的结果,并没有打印 "LazyService add to ioc container" 也就没有实例化 LazyService ,说明 @Lazy 注解起作用了,我们延迟了对象加入容器的时机

一个奇怪的现象

那么在实际情况中,我们会在 controller 中调用这个 service,即在 controller 中注入这个对象:

package com.startdusk.forgot.api.v1;

import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/v1/lazy")
public class LazyController {

    @Autowired
    private LazyService lazyService;

    @GetMapping("/test")
    public String test() {
        lazyService.print();
        return "Hello, world";
    }
}

保持 LazyService 中的 @Lazy,我们运行程序



发现 LazyService 居然被实例化了,难道打上 @Lazy 注解的类被注入之后 @Lazy 就不起作用了吗?我们来看下这个 LazyController 的代码,这里其实我们也把 LazyController 加入了容器(即打上了 @RestController 注解),在 SpringBoot 中,加入 IOC 容器就必须实例化对象,而实例化对象就要求,这个对象的里面的成员变量,方法都要被初始化,就包括 lazyService 这个要被注入的变量,即使 LazyService 打上了 @Lazy 注解。

解决这个问题,我们也需要在 LazyController 打上 @Lazy 注解,才能延迟加入 IOC 容器

package com.startdusk.forgot.api.v1;

import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/v1/lazy")
@Lazy
public class LazyController {

    @Autowired
    private LazyService lazyService;

    @GetMapping("/test")
    public String test() {
        lazyService.print();
        return "Hello, world";
    }
}

@Lazy 会延迟到什么时候

会延迟到对象被调用的时候。如,运行程序,访问 localhost:8080/v1/lazy/test 就是访问 test 这个方法,那么就会调用 lazyService.print() 就会打印出:


我们可以看到,当被注入的对象被调用的时候,才会把对象加入 IOC 容器,然后注入对象

使用 @Lazy 的缺点

我们来调整下代码:
LazyService 注释掉 @Service 和 @Lazy

package com.startdusk.forgot.service;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

// @Service
// @Lazy
public class LazyService {
    public LazyService() {
        System.out.println("LazyService add to ioc container");
    }

    public void print() {
        System.out.println("lazy");
    }
}

那么运行代码,再来访问下 localhost:8080/v1/lazy/test
,那么,程序会给我们抛一个错误:

Error creating bean with name 'lazyController': Unsatisfied dependency expressed through field 'lazyService';......

就是找不到这个这个注入的对象。那么这个报错是在程序运行中报错,而不是程序启动的时候就报错了。那么我们在 LazyController 中去掉 @Lazy

package com.startdusk.forgot.api.v1;

import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/v1/lazy")
public class LazyController {

    @Autowired
    private LazyService lazyService;

    @GetMapping("/test")
    public String test() {
        lazyService.print();
        return "Hello, world";
    }
}

启动程序,那么就会在程序启动的时候直接报错,不让你运行

Field lazyService in com.startdusk.forgot.api.v1.LazyController required a bean of type 'com.startdusk.forgot.service.LazyService' that could not be found.

相关文章

  • SpringBoot:使用 @Lazy 注解懒加载

    为什么需要懒加载? 我们知道,在 SpringBoot 应用程序启动的时候,会实例化一些对象加入到 IOC 容器里...

  • Dagger2 进阶使用

    目录: @Qualifier @Named 注解的作用 懒加载 Lazy 和 Provider @Binds 的作...

  • spring源码深度解析04

    @Lazy:懒加载注解 懒加载是针对于单实例bean 来说的。 当我们如下 可以看出容器启动的时候创建了bean实...

  • Objective-C | 谈谈我对懒加载的一点看法

    懒加载: lazy load,又名延迟加载,需要的时候才加载。 目前对于懒加载有下面三种说法: 1.禁止使用懒加载...

  • OC 谈谈我对懒加载的一点看法

    懒加载: lazy load,又名延迟加载,需要的时候才加载。 目前对于懒加载有下面三种说法: 1.禁止使用懒加载...

  • swift学习之懒加载

    在移动端开发过程之中,懒加载随处可见。在swift中,苹果推荐使用懒加载,如何实现懒加载呢? 关键字: lazy ...

  • Swfit 懒加载、闭包

    Swift有懒加载这么一说,而且Apple 很推荐我们使用懒加载, 并且还有一个关键字lazy懒加载定义: var...

  • Sping中bean的延迟加载

    Srping中的延迟加载使用@Lazy注解或把 中的lazy-init属性设置为true就可以了。之前只了解这么多...

  • lazy instantiation

    1.Lazy instantiation(懒加载) 先说一下什么是懒加载吧。懒加载—也称为延迟加载,即在需要...

  • 使用vue-lazyload实现图片懒加载

    使用vue-lazyload实现图片懒加载 1、安装插件 cnpm install --save vue-lazy...

网友评论

      本文标题:SpringBoot:使用 @Lazy 注解懒加载

      本文链接:https://www.haomeiwen.com/subject/ioksyhtx.html