• ABOUT
  • PORTFOLIO
  • POSTS
  • GUESTBOOK

ยฉ 2025-2026 BlueCool12 All rights reserved.

2026.01.12Spring

๐Ÿง‘โ€๐Ÿ”ง Lombok์˜ ์ƒ์„ฑ์ž ์–ด๋…ธํ…Œ์ด์…˜

Lombok์—๋Š” ์ƒ์„ฑ์ž ๊ด€๋ จ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋Œ€ํ‘œ์ ์œผ๋กœ 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

@NoArgsConstructor 
@AllArgsConstructor 
@RequiredArgsConstructor 

๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž.

1. @NoArgsConstructor 

์ฒซ ๋ฒˆ์งธ๋กœ @NoArgsConstructor ์–ด๋…ธํ…Œ์ด์…˜์˜ ๊ฒฝ์šฐ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด ์ด๋Ÿฐ ํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ๋•Œ 

@NoArgsConstructor
public class Post {
private Long id;
private String title;
private String content;
private LocalDateTime createdAt;
}

Lombok์€ ๋‚ด๋ถ€์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

public Post() {}

JPA ๊ฐ™์€ ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ Jackson ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์‚ฌ์‹ค์ƒ ํ•„์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.


2. @AllArgsConstructor

@AllArgsConstructor๋Š” ์ด๋ฆ„์—์„œ๋„ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

@AllArgsConstructor
public class Post {
private Long id;
private String title;
private String content;
private LocalDateTime createdAt;
}

์œ„์™€ ๊ฐ™์€ ํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค.

public Post(Long id, String title, String content, LocalDateTime createdAt) {
this.id = id;
this.title = title;
this.content = content;
this.createdAt = createdAt;
}

DTO์™€ ๊ฐ™์ด ๋ชจ๋“  ํ•„๋“œ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋‚˜ Builder ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.


3. @RequiredArgsConstructor

๋งˆ์ง€๋ง‰์œผ๋กœ @RequiredArgsConstructor๋Š” final ํ‚ค์›Œ๋“œ ๋˜๋Š” @NonNull์ด ๋ถ™์€ ํ•„๋“œ๋งŒ ํฌํ•จํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

@RequiredArgsConstructor
public class PostService {
private final PostRepository postRepository;
}

์œ„์˜ ๊ฐ™์€ ์ฝ”๋“œ์— ๋‚ด๋ถ€์ ์œผ๋กœ ์•„๋ž˜์˜ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.

public class PostService {
private final PostRepository postRepository;

public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
}

์ฃผ๋กœ Spring์—์„œ ์ƒ์„ฑ์ž ๊ธฐ๋ฐ˜ ์˜์กด์„ฑ ์ฃผ์ž…(DI)์„ ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค.


4. ์ƒ์„ฑ์ž ์–ด๋…ธํ…Œ์ด์…˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜

Lombok์˜ ์ƒ์„ฑ์ž ๊ด€๋ จ ์–ด๋…ธํ…Œ์ด์…˜์€ ๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜์„ ํ†ตํ•ด ์ƒ์„ฑ์ž์˜ ๋™์ž‘์„ ์ถ”๊ฐ€๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • access
    - ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค ๋•Œ ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. (PUBLIC, PRIVATE, PROTECTED ๋“ฑ)
    - @NoArgsConstructor(access = AccessLevel.PROTECTED) ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.
  • staticName
    - ์ง€์ •ํ•œ ์ด๋ฆ„์œผ๋กœ ์ •์ (static) ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ค€๋‹ค.
@AllArgsConstructor(staticName = "of")
public class Post {
private Long id;
private String title;
private String content;
private LocalDateTime createdAt;
}
// ์•„๋ž˜์™€ ๊ฐ™์€ ์ •์  ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
public static Post of(Long id, String title, String content, LocalDateTime createdAt) {
return new Post(id, title, content, createdAt);
}


  • force
    - final ํ•„๋“œ๋ฅผ ๊ฐ•์ œ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. 
    - ์˜ˆ๋ฅผ ๋“ค์–ด ํด๋ž˜์Šค์— final ํ•„๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์ƒ์„ฑ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ๋ฐ ์ด๊ฑธ ๊ฐ•์ œ๋กœ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
    - @AllArgsConstructor์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ƒ์„ฑ์ž์—์„œ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— force ์˜ต์…˜์ด ํ•„์š” ์—†๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์—†๋‹ค. 
  • onConstructor
    - ์ƒ์„ฑ์ž์— ๋ถ™์ผ ์ถ”๊ฐ€ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ง€์ •ํ•œ๋‹ค.
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PostService {
private final PostRepository postRepository;
}
// ์•„๋ž˜์˜ ํ˜•ํƒœ๋กœ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
@Autowired
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
ๅ‰ใฎ่จ˜ไบ‹
๐Ÿ’‰ Spring์œผ๋กœ ์•Œ์•„๋ณด๋Š” ์˜์กด์„ฑ ์ฃผ์ž…(DI)
ๆฌกใฎ่จ˜ไบ‹
๐Ÿž ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ Long ํƒ€์ž… ID (feat. NestJS)
่ฃ…้ฃพใƒญใ‚ด