项目依赖
<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
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(Application.class);
sa.setLazyInitialization(true);
sa.run(args);
}
}
写法2
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplicationBuilder sab = new SpringApplicationBuilder(Application.class);
sab.lazyInitialization(true).run(args);
}
}
写法3
直接在application.yml
文件中
spring:
main:
lazy-initialization: true
展示效果
Controller
层代码
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
层代码
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");
}
}
展示效果
启动项目后打印日志
访问localhost:8080
后打印的日志
配置成功
2. 局部类/方法
配置
想要实现局部类/方法
的懒加载很简单,只需要在类/方法
上添加@Lazy
注解即可,默认值true
开启懒加载,不开启可设置为false
,例如:想要懒加载IndexController
那么只需要在类上添加@Lazy
。注:我这里在展示局部实现效果时,项目中以去除全局懒加载的配置
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";
}
}
启动项目打印日志
调用localhost:8080
接口后打印的日志
**注:当设置懒加载的类/方法
有被其他类
初始化注入时,如果注入此类/方法
的类没有设置懒加载的话,在启动时一样会被初始话。**例如:给IndexService
添加@Lazy
懒加载注解,然后去掉IndexController
的懒加载注解后再启动项目查看日志
局部配置比全局配置优先级要高
-
在
application.yml
中设置开启全局懒加载配置spring: main: lazy-initialization: true
-
给
IndexService
通过@Lazy
设置禁用懒加载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"); } }
-
启动项目查看日志打印,可以看到
IndexService
在项目启动时就进行了初始化,而IndexController
在启动时并没有进行初始化
参考博客:https://juejin.im/post/5e83f795f265da47e449f4ea