Post

Lombok으로 Java 코드 간결하게 만들기: Getter, Setter, Constructor 활용법

계속해서 반복되는 getter, setter, constructor


Java 에서 class 를 작성하다 보면 정말 자주 반복되는 코드들이 보인다. 예를 들어 아래와 같이 class 가 하나 있다고 해보자

1
2
3
4
5
6
public class Post {
	private String id;
	private String title;
	private String content;
	...
}

이렇게 class 에 들어가는 instance 들이 여러개 있을것이다! 그럼 이제 여기에 맞도록 getter, setter, constructor 를 작성해 줘야한다!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Post {
	private String id;
	private String title;
	private String content;

	Post(String id, String title, String content) {
		this.id = id;
		this.title = title;
		this.content = content;
	}

	Post() {}

	public String getId () {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle () {
		return title;
	}
	
	public void setTitle(String title) {
        this.title = title;
	}

	public String getContent () {
		return content;
	}

	
	public void setContent(String content) {
        this.content = content;
	}
}

예를 들어보려고 작성한 class인데 instance 가 3개뿐이지만 벌써 작성하기가 귀찮고 힘이든다…

이럴때 사용하려고 Lombok 이 등장했다!!


lombok 은 이렇게 귀찮게 자주 작서하는 getter, setter, constructor 를 대신 작성해준다! 우선 pom.xml 에 추가해준다!

1
2
3
4
5
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>

@Getter 라고 Class 위에 작성을 해 주면 모든 instance 들에 대해 getter 를 작성해주고 instance 위에 작성을 하면 한개의 instance 에 대해 getter 를 작성해준다!

1
2
3
4
5
6
7
8
9
10
11
@Getter
public class Post {
	private String id;
	private String content; // 둘다 getter 가 있음!
}

public class Post {
	@Getter
	private String id; // id 만 getter 가 있음!
	private String content;
}

@Setter 도 마찬가지다!

1
2
3
4
5
6
7
8
9
10
11
@Setter
public class Post {
	private String id;
	private String content; // 둘다 setter 가 있음!
}

public class Post {
	@Setter
	private String id; // id 만 setter 가 있음!
	private String content;
}

벌써 이것만 해도 엄청난 코드가 삭제 되었다! 코드가 없어도 실제로는 사용이 가능하다!

@AllArgsConstructor 는 클래스 위에 쓸 수 있으며 모든 instance 를 가진 생성자를 생성해 준다. 그러니까 아래의 코드를 없애준다!

1
2
3
4
5
6
7
8
9
10
11
12
@AllArgsConstructor
public class Post {
	private String id;
	private String title;
	private String content;

	// Post(String id, String title, String content) {
	//	this.id = id;
	//	this.title = title;
	//	this.content = content;
	// }   이게 필요없다!!!
}

@NoArgsContructor 또한 클래스 위에 사용가능하고 아무런 instance 가 들어가지않은 생성자를 만들어준다!

1
2
3
4
5
6
7
8
9
@NoArgsConstructor
public class Post {
	private String id;
	private String title;
	private String content;

	// Post() {
	// }   이게 필요없다!!!
}

마지막으로 @RequiredArgsConstructor 은 내가 @NonNull 을 붙인 instance 들이 들어간 생성자를 만들어준다!

1
2
3
4
5
6
7
8
9
10
11
12
13
@RequiredArgsConstructor
public class Post {
	@NonNull
	private String id;
    @NonNull
	private String title;
	private String content;

	// Post(String id, String title) {
	// this.id = id;
	// this.title = title;
	// }   이게 필요없다!!!
}

Lombok 을 이용하면 정말 class 선언 파일을 깔끔하게 관리 할 수 있다!! 앞으로 애용해야지!!

This post is licensed under CC BY 4.0 by the author.

© 병욱. Some rights reserved.