@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修饰。


相关推荐
勇哥java实战分享4 小时前
程序员的明天:AI 时代下的行业观察与个人思考
后端
掘金码甲哥5 小时前
超性感的轻量级openclaw平替,我来给你打call
后端
用户8356290780518 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
啊哈灵机一动8 小时前
使用golang搭建一个nes 模拟器
后端
日月云棠9 小时前
各版本JDK对比:JDK 25 特性详解
java
间彧10 小时前
SpringBoot + ShardingSphere 读写分离实战指南
后端
砍材农夫10 小时前
订单超时
后端
树獭叔叔10 小时前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
用户83071968408210 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
得鹿10 小时前
MySQL基础架构与存储引擎、索引、事务、锁、日志
后端