Java中的@AllArgsConstructor注解(详解)

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;
  • 14
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码蒋

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值