s
s
spring
Search…
Lombok 활용하기

Gradle 설정

  • build.gradle에 다음 롬복설정을 추가해준다.
1
configurations {
2
compileOnly {
3
extendsFrom annotationProcessor
4
}
5
}
6
7
repositories {
8
mavenCentral()
9
}
10
11
dependencies {
12
compileOnly 'org.projectlombok:lombok:1.18.20'
13
annotationProcessor 'org.projectlombok:lombok:1.18.20'
14
15
testCompileOnly 'org.projectlombok:lombok:1.18.20'
16
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
17
}
Copied!
추가후에 반드시 아래 설정을 해줘야한다.
  • Preferences -> Compiler -> Annotation Processors
image-20210515235710302
Enable annotation processing을 반드시 설정해줘야한다.

@Getter / @Setter

접근자(getXxx())와 설정자(setXxx())를 자동으로 생성해준다.
1
package dh0023.springcore.member.domain;
2
3
import lombok.Getter;
4
import lombok.Setter;
5
6
@Getter
7
@Setter
8
public class Member {
9
10
private Long id;
11
private String name;
12
private Grade grade;
13
14
}
Copied!
다음과 같이 자동으로 getter와 setter가 생성된 것을 확인할 수 있다.
image-20210516001538291

생성자 자동 생성

@NoArgsConstructor

파라미터가 없는 생성자를 자동으로 생성해준다.

@AllArgsConstructor

모든 필드 값을 파라미터로 받는 생성자를 생성해준다.
1
@NoArgsConstructor
2
@AllArgsConstructor
3
public class Member {
4
5
private Long id;
6
private String name;
7
private Grade grade;
8
9
}
Copied!
image-20210516001852822

@RequiredArgsConstructor

@RequiredArgsConstructor 어노테이션은 final이나 @NonNull인 필드 값만 파라미터로 받는 생성자를 만들어준다.
이렇게 생성하면, 코드가 훨씬 깔끔해지고 간편한 것을 확인할 수 있다.
1
@Component
2
public class OrderServiceImpl implements OrderService{
3
4
// final은 반드시 값이 있어야한다.
5
private final MemberRepository memberRepository;
6
private final DiscountPolicy discountPolicy;
7
8
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
9
this.memberRepository = memberRepository;
10
this.discountPolicy = discountPolicy;
11
}
12
}
Copied!
위와 같이 생성자를 직접 만들어줄 필요 없이 해당 어노테이션만 별도로 설정해주면 된다.
1
@Component
2
@RequiredArgsConstructor
3
public class OrderServiceImpl implements OrderService{
4
5
// final은 반드시 값이 있어야한다.
6
private final MemberRepository memberRepository;
7
private final DiscountPolicy discountPolicy;
8
}
Copied!
intelliJ에서 command + F12로 해당 클래스의 메서드와 변수를 확인할 수 있는데, 아래와 같이 생성자가 생성된 것을 확인할 수 있다.
image-20210516001012197

@ToString

toString() 메소드도 lombok의 @ToString 어노테이션으로 간단하게 생성할 수 있다. 또한, exclude 옵션으로 특정 필드를 제외할 수 있다.
1
@Getter
2
@Setter
3
@ToString(exclude = "grade")
4
@AllArgsConstructor
5
public class Member {
6
7
private Long id;
8
private String name;
9
private Grade grade;
10
11
}
Copied!
1
public class LombokTest {
2
@Test
3
@DisplayName("toString() 테스트")
4
void getToString() {
5
Member member = new Member(1L, "test123", Grade.VIP);
6
7
System.out.println(member);
8
}
9
}
Copied!
1
Member(id=1, name=test123)
2
3
Process finished with exit code 0
Copied!
exclude 에 포함된 필드를 제외하고 전체 출력되는 것을 확인할 수 있다.

@Builder

@Builder 로 단순하게 빌더를 생성할 수 있다.
1
public class Item {
2
private final String itemCd; // 필수
3
private final String itemNm; // 필수
4
private final String ctgId; // 필수
5
private final BigDecimal price; // 선택
6
private final String sellTypeCd; // 선택
7
8
public static class Builder {
9
private final String itemCd; // 필수
10
private final String itemNm; // 필수
11
private final String ctgId; // 필수
12
13
// 선택적 매개변수는 default 값으로 초기화
14
private BigDecimal price = BigDecimal.ZERO;
15
private String sellTypeCd = "00";
16
17
public Builder(String itemCd, String itemNm, String ctgId){
18
this.itemCd = itemCd;
19
this.itemNm = itemNm;
20
this.ctgId = ctgId;
21
}
22
public Builder price(BigDecimal price){
23
this.price = price;
24
return this;
25
}
26
27
public Builder sellTypeCd(String sellTypeCd){
28
this.sellTypeCd = sellTypeCd;
29
return this;
30
}
31
32
public Item build(){
33
return new Item(this);
34
}
35
}
36
37
private Item(Builder builder){
38
itemCd = builder.itemCd;
39
itemNm = builder.itemNm;
40
ctgId = builder.ctgId;
41
price = builder.price;
42
sellTypeCd = builder.sellTypeCd;
43
}
44
}
Copied!
다음과 같이 Item.Builder를 단순히 @Builder 어노테이션 만으로 구현할 수 있다.
1
@Builder
2
public class Item {
3
private final String itemCd; // 필수
4
private final String itemNm; // 필수
5
private final String ctgId; // 필수
6
private final BigDecimal price; // 선택
7
private final String sellTypeCd; // 선택
8
}
Copied!
1
//
2
// Source code recreated from a .class file by IntelliJ IDEA
3
// (powered by FernFlower decompiler)
4
//
5
6
package ch2.dahye.item2;
7
8
import java.math.BigDecimal;
9
10
public class Item {
11
private final String itemCd;
12
private final String itemNm;
13
private final String ctgId;
14
private final BigDecimal price;
15
private final String sellTypeCd;
16
17
Item(String itemCd, String itemNm, String ctgId, BigDecimal price, String sellTypeCd) {
18
this.itemCd = itemCd;
19
this.itemNm = itemNm;
20
this.ctgId = ctgId;
21
this.price = price;
22
this.sellTypeCd = sellTypeCd;
23
}
24
25
public static Item.ItemBuilder builder() {
26
return new Item.ItemBuilder();
27
}
28
29
public static class ItemBuilder {
30
private String itemCd;
31
private String itemNm;
32
private String ctgId;
33
private BigDecimal price;
34
private String sellTypeCd;
35
36
ItemBuilder() {
37
}
38
39
public Item.ItemBuilder itemCd(String itemCd) {
40
this.itemCd = itemCd;
41
return this;
42
}
43
44
public Item.ItemBuilder itemNm(String itemNm) {
45
this.itemNm = itemNm;
46
return this;
47
}
48
49
public Item.ItemBuilder ctgId(String ctgId) {
50
this.ctgId = ctgId;
51
return this;
52
}
53
54
public Item.ItemBuilder price(BigDecimal price) {
55
this.price = price;
56
return this;
57
}
58
59
public Item.ItemBuilder sellTypeCd(String sellTypeCd) {
60
this.sellTypeCd = sellTypeCd;
61
return this;
62
}
63
64
public Item build() {
65
return new Item(this.itemCd, this.itemNm, this.ctgId, this.price, this.sellTypeCd);
66
}
67
68
public String toString() {
69
return "Item.ItemBuilder(itemCd=" + this.itemCd + ", itemNm=" + this.itemNm + ", ctgId=" + this.ctgId + ", price=" + this.price + ", sellTypeCd=" + this.sellTypeCd + ")";
70
}
71
}
72
}
Copied!

상속 받은 클래스 Builder 구현

1
@Getter
2
@AllArgsConstructor
3
public class Parent {
4
private final String parentName;
5
private final int parentAge;
6
}
7
8
@Getter
9
public class Child extends Parent {
10
private final String childName;
11
private final int childAge;
12
13
@Builder
14
public Child(String parentName, int parentAge, String childName, int childAge) {
15
super(parentName, parentAge);
16
this.childName = childName;
17
this.childAge = childAge;
18
}
19
}
Copied!
상속받은 클래스에서는 다음과 같이 생성자에 Builder 어노테이션을 추가해 Builder를 구현할 수 있다.
1
public class Child extends Parent {
2
private final String childName;
3
private final int childAge;
4
5
public Child(String parentName, int parentAge, String childName, int childAge) {
6
super(parentName, parentAge);
7
this.childName = childName;
8
this.childAge = childAge;
9
}
10
11
public static Child.ChildBuilder builder() {
12
return new Child.ChildBuilder();
13
}
14
15
public String getChildName() {
16
return this.childName;
17
}
18
19
public int getChildAge() {
20
return this.childAge;
21
}
22
23
public static class ChildBuilder {
24
private String parentName;
25
private int parentAge;
26
private String childName;
27
private int childAge;
28
29
ChildBuilder() {
30
}
31
32
public Child.ChildBuilder parentName(String parentName) {
33
this.parentName = parentName;
34
return this;
35
}
36
37
public Child.ChildBuilder parentAge(int parentAge) {
38
this.parentAge = parentAge;
39
return this;
40
}
41
42
public Child.ChildBuilder childName(String childName) {
43
this.childName = childName;
44
return this;
45
}
46
47
public Child.ChildBuilder childAge(int childAge) {
48
this.childAge = childAge;
49
return this;
50
}
51
52
public Child build() {
53
return new Child(this.parentName, this.parentAge, this.childName, this.childAge);
54
}
55
56
public String toString() {
57
return "Child.ChildBuilder(parentName=" + this.parentName + ", parentAge=" + this.parentAge + ", childName=" + this.childName + ", childAge=" + this.childAge + ")";
58
}
59
}
60
}
Copied!
다음과 같이 부모 클래스의 필드까지 모두 Builder 내부에 구현된 것을 확인할 수 있다.
1
Child child = Child.builder()
2
.parentName("Andrea")
3
.parentAge(38)
4
.childName("Emma")
5
.childAge(6)
6
.build();
Copied!

@SuperBuilder

Lombok 1.18.2 이후부터는 @SuperBuilder 어노테이션으로 상속되는 클래스의 Builder를 생성할 수 있다.
1
@SuperBuilder
2
public class Parent {
3
private final String parentName;
4
private final int parentAge;
5
}
6
7
@SuperBuilder
8
public class Child extends Parent {
9
private final String childName;
10
private final int childAge;
11
}
Copied!
1
public class Parent {
2
private final String parentName;
3
private final int parentAge;
4
5
protected Parent(Parent.ParentBuilder<?, ?> b) {
6
this.parentName = b.parentName;
7
this.parentAge = b.parentAge;
8
}
9
10
public static Parent.ParentBuilder<?, ?> builder() {
11
return new Parent.ParentBuilderImpl();
12
}
13
14
private static final class ParentBuilderImpl extends Parent.ParentBuilder<Parent, Parent.ParentBuilderImpl> {
15
private ParentBuilderImpl() {
16
}
17
18
protected Parent.ParentBuilderImpl self() {
19
return this;
20
}
21
22
public Parent build() {
23
return new Parent(this);
24
}
25
}
26
27
public abstract static class ParentBuilder<C extends Parent, B extends Parent.ParentBuilder<C, B>> {
28
private String parentName;
29
private int parentAge;
30
31
public ParentBuilder() {
32
}
33
34
protected abstract B self();
35
36
public abstract C build();
37
38
public B parentName(String parentName) {
39
this.parentName = parentName;
40
return this.self();
41
}
42
43
public B parentAge(int parentAge) {
44
this.parentAge = parentAge;
45
return this.self();
46
}
47
48
public String toString() {
49
return "Parent.ParentBuilder(parentName=" + this.parentName + ", parentAge=" + this.parentAge + ")";
50
}
51
}
52
}
Copied!
1
public class Child extends Parent {
2
private final String childName;
3
private final int childAge;
4
5
protected Child(Child.ChildBuilder<?, ?> b) {
6
super(b);
7
this.childName = b.childName;
8
this.childAge = b.childAge;
9
}
10
11
public static Child.ChildBuilder<?, ?> builder() {
12
return new Child.ChildBuilderImpl();
13
}
14
15
private static final class ChildBuilderImpl extends Child.ChildBuilder<Child, Child.ChildBuilderImpl> {
16
private ChildBuilderImpl() {
17
}
18
19
protected Child.ChildBuilderImpl self() {
20
return this;
21
}
22
23
public Child build() {
24
return new Child(this);
25
}
26
}
27
28
public abstract static class ChildBuilder<C extends Child, B extends Child.ChildBuilder<C, B>> extends ParentBuilder<C, B> {
29
private String childName;
30
private int childAge;
31
32
public ChildBuilder() {
33
}
34
35
protected abstract B self();
36
37
public abstract C build();
38
39
public B childName(String childName) {
40
this.childName = childName;
41
return this.self();
42
}
43
44
public B childAge(int childAge) {
45
this.childAge = childAge;
46
return this.self();
47
}
48
49
public String toString() {
50
String var10000 = super.toString();
51
return "Child.ChildBuilder(super=" + var10000 + ", childName=" + this.childName + ", childAge=" + this.childAge + ")";
52
}
53
}
54
}
Copied!
ChildBuilder는 ParentBuilder를 상속받고 있는것을 볼 수 있다.

참고