@Lazy注解使用注意事项

说明:在使用@Autowired注解,自动装配某Bean时,为了避免循环依赖(即自己注入自己,或两个Bean之间互相注入),可使用@Lazy注解,使其不在项目启动的时候就注入Bean,而是在首次使用的时候再注入,即懒加载,可以解决循环依赖的问题。

本文介绍使用@Lazy可能会出现的空指针异常(NPE)。

场景

如下,在 AsyncServiceImpl 内调用本类的方法,自己注入了自己

java 复制代码
import com.hezy.mapper.UserMapper;
import com.hezy.pojo.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class AsyncServiceImpl {

    @Autowired
    @Lazy
    private AsyncServiceImpl asyncService;

    @Autowired
    private UserMapper userMapper;

    public void insertDTO1(UserDTO userDTO) {
        asyncService.insert(userDTO);
    }

    private void insert(UserDTO userDTO) {
        userMapper.insertUser(userDTO);
    }
}

这种情况,如果不加 @Lazy 注解,启动项目,会报循环依赖错误

问题

启动项目,发现在这里发生了空指针异常

解决

排查下来是因为成员方法insert()为private修饰,换成public就行了,如下:

分析

推测可能与动态代理的方式有关,CGLIB动态代理要求被代理的类或方法不能被final修饰。

对于上述场景,除了修改成员方法的修饰符,把private改为public,还可以考虑不走代理,而直接调用成员方法,如下,都不需要注入自己。

java 复制代码
    @Autowired
    private UserMapper userMapper;

    public void insertDTO1(UserDTO userDTO) {
        insert(userDTO);
    }

    public void insert(UserDTO userDTO) {
        userMapper.insertUser(userDTO);
    }

大家可能有两个问题:


问:为什么要自己注入自己?直接调用成员方法不就行了?

答:因为要考虑到事务,如果用声明式注解,直接调用成员方法,事务会失效。上述代码没有事务问题,故可以直接调用成员方法。


问:为什么方法要用private修饰?直接用public多省事。

答:因为规范,成员方法如果只在本类中使用,须用private修饰。


相关推荐
程序员-周李斌13 小时前
Java NIO [非阻塞 + 多路复用解]
java·开发语言·开源软件·nio
程序猿小蒜13 小时前
基于Spring Boot的宠物领养系统的设计与实现
java·前端·spring boot·后端·spring·宠物
合作小小程序员小小店13 小时前
web网页开发,在线%食堂管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·mysql·html·intellij-idea·jquery
皮皮林55113 小时前
SpringBoot + Spring AI 玩转智能应用开发
spring boot
奋斗的小高13 小时前
Docker 安装与使用
java
w***48113 小时前
Springboot项目本地连接并操作MySQL数据库
数据库·spring boot·mysql
毕设源码-钟学长13 小时前
【开题答辩全过程】以 浮生馆汉服租赁管理系统为例,包含答辩的问题和答案
android·java·tomcat
90后小陈老师13 小时前
用户管理系统 07 项目前端初始化 | 新手实战 | 期末实训 | Java+SpringBoot+Vue
java·前端·spring boot
k***825113 小时前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
tan180°13 小时前
Linux网络TCP(上)(11)
linux·网络·c++·后端·tcp/ip