简体   繁体   中英

Lombok @Builder with setter

I have class having Lombok @Builder annotation as

@Builder
public class Employee {

  private String empId;
  private String empName;
  private String managerName;
}

After processing or after some business logic I want set managerName in same Employee Object by using setter method like empObj.setManagerName("managerName")

How can I use setter method with Builder in Lombok

You have options. Check out the Lombok documentation

If you want just managerName to have a setter:

Use the Lombok @Setter on managerName , ie

@Setter
private String managerName;

If you want all your fields to have a setter:

You can also put a @Getter and/or @Setter annotation on a class. In that case, it's as if you annotate all the non-static fields in that class with the annotation.

You can also add the class-level @Data annotation . It gives you the combined effect of several other annotations, and is often useful for POJO's.

NB that @Data and @Builder play a little rough with one-another, because @Data removes the default no-arg constructor. So, if you want to keep @Builder , you'll need to add an explicit @NoArgsConstructor .

Just add @Setter to class/field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM