SpringBoot2.2.X版本懒加载的实现方法

项目依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

1. 全局配置

写法1

1
2
3
4
5
6
7
8
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(Application.class);
sa.setLazyInitialization(true);
sa.run(args);
}
}

写法2

1
2
3
4
5
6
7
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplicationBuilder sab = new SpringApplicationBuilder(Application.class);
sab.lazyInitialization(true).run(args);
}
}

写法3

直接在application.yml文件中

1
2
3
spring:
main:
lazy-initialization: true

展示效果

Controller层代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.lx.controller;

import com.lx.service.IndexService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Lazy(false)
@RestController
public class IndexController {
public IndexController() {
log.info("[{}]开始初始化", IndexController.class);
}

@Autowired
private IndexService indexService;

@GetMapping("/")
public String index() {
indexService.index();
return "success";
}
}

Service层代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.lx.service;

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

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Lazy
@Service
public class IndexService {
public IndexService() {
log.info("[{}]开始初始化", IndexService.class);
}

public void index() {
log.info("index method run");
}
}

展示效果

启动项目后打印日志

GTThvt.png

访问localhost:8080后打印的日志

GTTzrV.png

配置成功

2. 局部类/方法配置

想要实现局部类/方法的懒加载很简单,只需要在类/方法上添加@Lazy注解即可,默认值true开启懒加载,不开启可设置为false,例如:想要懒加载IndexController那么只需要在类上添加@Lazy注:我这里在展示局部实现效果时,项目中以去除全局懒加载的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.lx.controller;

import com.lx.service.IndexService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Lazy
@Slf4j
@RestController
public class IndexController {
public IndexController() {
log.info("[{}]开始初始化", IndexController.class);
}

@Autowired
private IndexService indexService;

@GetMapping("/")
public String index() {
indexService.index();
return "success";
}
}

启动项目打印日志

GTbn4e.png

调用localhost:8080接口后打印的日志

GTbQgA.png

注:当设置懒加载的类/方法有被其他类初始化注入时,如果注入此类/方法的类没有设置懒加载的话,在启动时一样会被初始话。例如:给IndexService添加@Lazy懒加载注解,然后去掉IndexController的懒加载注解后再启动项目查看日志

GTLiex.png

局部配置比全局配置优先级要高

  1. application.yml中设置开启全局懒加载配置

    1
    2
    3
    spring:
    main:
    lazy-initialization: true
  1. IndexService通过@Lazy设置禁用懒加载

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.lx.service;

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

    import lombok.extern.slf4j.Slf4j;

    @Lazy
    @Slf4j
    @Service
    public class IndexService {
    public IndexService() {
    log.info("[{}]开始初始化", IndexService.class);
    }

    public void index() {
    log.info("index method run");
    }
    }
  1. 启动项目查看日志打印,可以看到IndexService在项目启动时就进行了初始化,而IndexController在启动时并没有进行初始化

    GTOGHx.png

参考博客:https://juejin.im/post/5e83f795f265da47e449f4ea

作者: 只是学习学习
邮箱: fengzxia1000@163.com
原文地址: https://fengzxia.gitee.io/posts/53d4b364.html
版权声明: 商业转载请联系作者获得授权,非商业转载请注明出处。