首页 > 基础资料 博客日记
详细分析Java中的@AllArgsConstructor注解
2024-06-22 13:00:05基础资料围观625次
这篇文章介绍了详细分析Java中的@AllArgsConstructor注解,分享给大家做个参考,收藏Java资料网收获更多编程知识
前言
事情起因是Spring的循环依赖
详情可见:出现The dependencies of some of the beans in the application context form a cycle 解决方法(全)
1. 基本知识
@AllArgsConstructor
是 Lombok 提供的一个注解,用于自动生成一个包含所有参数的构造函数
简化构造函数的编写:通常情况下,Java 类中需要手动编写构造函数来初始化类的实例变量
使用 @AllArgsConstructor
注解可以自动生成一个包含所有实例变量的构造函数,减少编码量
使用方法
- 引入依赖:在项目的依赖中加入 Lombok 库
- 在类上添加注解:在需要使用该注解的类上添加
@AllArgsConstructor
注解
示例:
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class MyClass {
private String field1;
private int field2;
private double field3;
}
生成的构造函数如下:
public MyClass(String field1, int field2, double field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
2. 实战
对于@Service注解中也可使用
@Service
@AllArgsConstructor
public class WireRopeReplaceOrderServiceImpl extends BaseServiceImpl<WireRopeReplaceOrderMapper, WireRopeReplaceOrder> implements IWireRopeReplaceOrderService {
private IInfoService infoService;
private IInfoRunningDataService iInfoRunningDataService;
private IInfoWireropeDataService infoWireropeDataService;
private IInfoRunningDataRopeService infoRunningDataRopeService;
private RedisIdGeneratorService redisIdGeneratorService;
类似如下:
@Service
public class WireRopeReplaceOrderServiceImpl extends BaseServiceImpl<WireRopeReplaceOrderMapper, WireRopeReplaceOrder> implements IWireRopeReplaceOrderService {
@Autowired
private IInfoService infoService;
@Autowired
private IInfoRunningDataService iInfoRunningDataService;
@Autowired
private IInfoWireropeDataService infoWireropeDataService;
@Autowired
private IInfoRunningDataRopeService infoRunningDataRopeService;
@Autowired
private RedisIdGeneratorService redisIdGeneratorService;
文章来源:https://blog.csdn.net/weixin_47872288/article/details/138509116
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- springboot~3.x项目中使用集成测试
- Java测试类、工具类与JavaBean对比解析
- SpringBoot-日志
- springboot~http2的支持
- 解疑释惑 - 日志体系之 slf4j + logback 组合(一)
- Web server failed to start. Port 8080 was already in use. 端口被占用
- Springboot 项目配置多数据源
- 伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 05
- 剑指offer-23、搜索⼆叉树的后序遍历序列
- 一个表示金额的数字是 100000000L,这是多少米?