如何使用带有扩展字段的 Lombok 创建实例
How to create instance with Lombok with extend field
我有下一个问题。我有这个方法:
public void sendDeletedEvent(final Comment comment) {
log.info("SendDeletedEvent.E with Comment: {}", comment);
final CommentDTO commentDTO = commentMapper.toDTO(comment);
final CommentDeleteEvent commentDeleteEvent = new CommentDeleteEvent(commentDTO);
kafkaTemplate.send(commentEventTopic, commentDeleteEvent);
log.info("SendDeleteEvent.X with CommentDeleteEvent : {}", commentDeleteEvent);
}
我想替换 lombok builder 上的所有新创建的内容,但我遇到了一些问题。这是我的 CommentDeleteEvent class:
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@Builder
@Data
public class CommentDeleteEvent extends AbstractCommentEvent {
public CommentDeleteEvent(CommentDTO comment) {
super(DELETE, comment);
}
}
和抽象事件
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public abstract class AbstractCommentEvent extends AbstractEvent {
private CommentDTO comment;
public AbstractCommentEvent(EventType eventType, CommentDTO comment) {
super(eventType);
this.comment = comment;
}
}
因此,当我尝试构建时,构建器看不到抽象的 CommentDTO 字段,我该怎么办?我可以做下一个代码,但我认为这是个坏主意:
log.info("SendDeletedEvent.E with Comment: {}", comment);
final CommentDTO commentDTO = commentMapper.toDTO(comment);
final CommentDeleteEvent commentDeleteEvent = (CommentDeleteEvent) AbstractCommentEvent.builder()
.comment(commentDTO)
.build();
kafkaTemplate.send(commentEventTopic, commentDeleteEvent);
log.info("SendDeleteEvent.X with CommentDeleteEvent : {}", commentDeleteEvent);
我该怎么办,或者我的想法不是最糟糕的?)
您想要使用的是 @SuperBuilder
而不是 @Builder
。 @SuperBuilder
使用来自 super类 的字段。此注释也应添加到 super 和 sub 类。
此外,由于显而易见的原因,我们不能在抽象超类上调用 builder
。构建器无法知道要实例化哪个子类。这不是 Lombok 的限制,这就是 OOP 的工作方式。
因此,我们应该按如下方式调用构建器:
final CommentDeleteEvent commentDeleteEvent = CommentDeleteEvent.builder()
.comment(commentDTO)
.build();
我有下一个问题。我有这个方法:
public void sendDeletedEvent(final Comment comment) {
log.info("SendDeletedEvent.E with Comment: {}", comment);
final CommentDTO commentDTO = commentMapper.toDTO(comment);
final CommentDeleteEvent commentDeleteEvent = new CommentDeleteEvent(commentDTO);
kafkaTemplate.send(commentEventTopic, commentDeleteEvent);
log.info("SendDeleteEvent.X with CommentDeleteEvent : {}", commentDeleteEvent);
}
我想替换 lombok builder 上的所有新创建的内容,但我遇到了一些问题。这是我的 CommentDeleteEvent class:
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@Builder
@Data
public class CommentDeleteEvent extends AbstractCommentEvent {
public CommentDeleteEvent(CommentDTO comment) {
super(DELETE, comment);
}
}
和抽象事件
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public abstract class AbstractCommentEvent extends AbstractEvent {
private CommentDTO comment;
public AbstractCommentEvent(EventType eventType, CommentDTO comment) {
super(eventType);
this.comment = comment;
}
}
因此,当我尝试构建时,构建器看不到抽象的 CommentDTO 字段,我该怎么办?我可以做下一个代码,但我认为这是个坏主意:
log.info("SendDeletedEvent.E with Comment: {}", comment);
final CommentDTO commentDTO = commentMapper.toDTO(comment);
final CommentDeleteEvent commentDeleteEvent = (CommentDeleteEvent) AbstractCommentEvent.builder()
.comment(commentDTO)
.build();
kafkaTemplate.send(commentEventTopic, commentDeleteEvent);
log.info("SendDeleteEvent.X with CommentDeleteEvent : {}", commentDeleteEvent);
我该怎么办,或者我的想法不是最糟糕的?)
您想要使用的是 @SuperBuilder
而不是 @Builder
。 @SuperBuilder
使用来自 super类 的字段。此注释也应添加到 super 和 sub 类。
此外,由于显而易见的原因,我们不能在抽象超类上调用 builder
。构建器无法知道要实例化哪个子类。这不是 Lombok 的限制,这就是 OOP 的工作方式。
因此,我们应该按如下方式调用构建器:
final CommentDeleteEvent commentDeleteEvent = CommentDeleteEvent.builder()
.comment(commentDTO)
.build();