Lombok(롬복) - 기본 설명
① lombok - Java 기반에서 기계적으로 작성하는 VO, DTO, Entity 관련 작업을 보다 쉽게 하게 해주는 도구입니다.
② Getter, Setter, ToString, hashCode 관련 메소드 작업 관련 Class(클래스) 코드를 깔끔하게 작성할 수 있습니다.
③ Spring(SpringSTS) 프로젝트에서 사용할 경우 JPA 환경과 함께 일관화 되고 가독성이 좋은 애플리케이션을 작성할 수 있습니다.
④ 단점은 협업 모든 인원이 lombok을 설치해야 한다는 것과 추가 어노테이션 사용할 경우 소스코드 분석이 난해해지는 것 등이 있습니다.
⑤ 설치 방법을 모르시는 분은 본 블로그 하단에서 참고하실 수 있습니다.(예제 소스코드 다운로드 가능)
Lombok(롬복) - 일반적인 클래스 VS lombok 클래스 예제 비교
기본 소스 - Lombok 미사용- 주로 DTO, VO, Entity 관련 Class- 물론 IDE(Eclipse, IntelliJ 등)에서도 자동으로 쉽게 작성 가능하다.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | public class NonLombokModel { private String name; private String age; private String address; public NonLombokModel(String name, String age, String address) { super(); this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "NonLombokModel [name=" + name + ", age=" + age + ", address=" + address + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((address == null) ? 0 : address.hashCode()); result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; NonLombokModel other = (NonLombokModel) obj; if (address == null) { if (other.address != null) return false; } else if (!address.equals(other.address)) return false; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } | cs |
기본 소스 - Lombok 사용- 매우 직관적으로 깔끔한 코드를 작성할 수 있다.- 일반적으로 @Data 어노테이션만 사용하는 경우가 많다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import lombok.*; //@Data로 사용 가능 @Getter @Setter @ToString @EqualsAndHashCode @Builder public class LombokModel { private @NonNull String name; private @NonNull String age; private @NonNull String address; } | cs |
기본 소스 - 실행- 실행 결과는 같다.
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 | import lom_bok.LombokModel.LombokModelBuilder; public class MainClass { public static void main(String[] args) { LombokModel lombokModel = new LombokModelBuilder() .name("Park") .address("seoul") .age("33") .build(); NonLombokModel nonLombokModel = new NonLombokModel("kim","Pusan","34"); //Lombok 사용 System.out.println("lombok library 사용!"); System.out.println("lombokModel : " + lombokModel); System.out.println("lombokModel : " + lombokModel.toString()); System.out.println("lombokModel : " + lombokModel.hashCode()); System.out.println(""); //Java Bean (Classic Way) 사용 System.out.println("lombok library 미사용!"); System.out.println("nonLombokModel : " + nonLombokModel); System.out.println("nonLombokModel : " + nonLombokModel.toString()); System.out.println("nonLombokModel : " + nonLombokModel.hashCode()); } } | cs |
실행 화면
Lombok에 대한 Reference 문서는 아래 URL을 참고하세요.
- https://projectlombok.org/features/all
Lombok 다운 및 설치 방법은 아래 블로그를 방문하시면 잘 설명되어 있습니다.
- http://duzi077.tistory.com/142
- http://countryxide.tistory.com/16
- https://projectlombok.org/features/all
Lombok 다운 및 설치 방법은 아래 블로그를 방문하시면 잘 설명되어 있습니다.
- http://duzi077.tistory.com/142
- http://countryxide.tistory.com/16
'웹 백엔드 > Spring' 카테고리의 다른 글
Spring Boot 기본 설정(2) - 프로젝트 생성 테스트 및 포트(Port) 변경 (0) | 2017.10.13 |
---|---|
Spring Boot 기본 설정(1) - Spring STS 다운로드 및 Gradle 설치 (0) | 2017.10.12 |