当前位置: 代码网 > it编程>编程语言>Java > 如何使用Lombok进行spring 注入

如何使用Lombok进行spring 注入

2025年07月17日 Java 我要评论
lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter注入使用 lombok 进行setter注入(尽量优先使用setter注入)@service@se

lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter注入

使用 lombok 进行setter注入(尽量优先使用setter注入)

@service
@setter(onmethod_ = {@autowired})
public class testserviceimpl implements testservice {
    private testdao testdao;
}

看一下编译的内容

@service
public class testserviceimpl implements testservice {
    private testdao testdao;
    @autowired
    public void settestdao(final testdao testdao) {
        this.testdao= testdao;
    }
}

使用 lombok 进行构造器注入

@service
@requiredargsconstructor(onconstructor_ = {@autowired})
public class testserviceimpl implements testservice {
    private final testdao testdao;
}

 或

@service
@requiredargsconstructor(onconstructor_ = {@autowired})
public class testserviceimpl implements testservice {
    @lombok.nonnull
    private testdao testdao;
}

编译的内容

@service
public class testserviceimpl implements testservice {
    private testdao testdao;
    @autowired
    public void testserviceimpl(final testdao testdao) {
        this.testdao= testdao;
    }
}

到此这篇关于优雅的使用lombok进行spring 注入的文章就介绍到这了,更多相关lombok spring 注入内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com