Lombok์ด๋
Lombok์ Java ์ธ์ด๋ฅผ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, ๋ฐ๋ณต์ ์ด๊ณ ์ฅํฉํ ์ฝ๋๋ฅผ ์ค์ด๊ณ ์ ๋ง๋ค์ด์ง ํ๋ก์ ํธ์ ๋๋ค. ์ฃผ์ ๋ชฉํ๋ ์ฝ๋ ์์ฑ์ ๊ฐ์ํํ๊ณ ๊ฐ๋ ์ฑ์ ํฅ์์์ผ ๊ฐ๋ฐ์๋ค์ด ๋ณด๋ค ํจ์จ์ ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋๋ก ๋๋ ๊ฒ
Lombok์ ์ฃผ๋ก ํ๋ก์ ํธ์ ์์กด์ฑ์ ์ถ๊ฐํ์ฌ ์ฌ์ฉํ๋ฉฐ, IDE์ Lombok ํ๋ฌ๊ทธ์ธ์ ์ค์นํ์ฌ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2. gradle
dependencies {
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor'org.projectlombok:lombok:1.18.20'
}
import lombok.Data;
@Data
public class Example {
private String field1;
private int field2;
}โ
2. `@Getter` / `@Setter`: Getter ๋๋ Setter ๋ฉ์๋๋ฅผ ์์ฑํฉ๋๋ค.
import lombok.Getter;
import lombok.Setter;
public class Example {
@Getter @Setter private String field;
}โ
3. `@NoArgsConstructor`, `@AllArgsConstructor`: ํ๋ผ๋ฏธํฐ๊ฐ ์๋ ์์ฑ์ ๋๋ ๋ชจ๋ ํ๋๋ฅผ ์ธ์๋ก ๋ฐ๋ ์์ฑ์๋ฅผ ์์ฑํฉ๋๋ค.
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@NoArgsConstructor // ํ๋ผ๋ฏธํฐ ์๋
@AllArgsConstructor // ๋ชจ๋ ๋ฐ๋
public class Example {
private String field1;
private int field2;
}โ
4. `@Builder`: ๋น๋ ํจํด์ ์์ฑํฉ๋๋ค.
import lombok.Builder;
@Builder
public class Example {
private String field1;
private int field2;
}โ
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class Person {
private String name;
private int age;
private String address;
}
// ์ฌ์ฉ ์์
Person person = Person.builder()
.name("John")
.age(30)
.address("123 Main St")
.build();
5. `@Slf4j`: ๊ฐ๋จํ ๋ก๊น ์ ์ํด SLF4J Logger๋ฅผ ์์ฑํฉ๋๋ค.
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Example {
public void exampleMethod() {
log.info("Logging with Lombok @Slf4j");
}
}โ
6. `@RequiredArgsConstructor`:ํด๋์ค์ ๋ชจ๋ final ํ๋๋ @NonNull ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํ๋์ ๋ํ ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํฉ๋๋ค. ์ด๋ ๊ฒ ์์ฑ๋ ์์ฑ์๋ ํด๋น ํ๋๋ค์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ ์ด๊ธฐํํ๋ ์ญํ ์ ํฉ๋๋ค.
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class Example {
private final int id;
private final String name;
private final double price;
// ์๋์ผ๋ก ์์ฑ๋ ์์ฑ์
// public Example(int id, String name, double price) {
// this.id = id;
// this.name = name;
// this.price = price;
// }
}
https://haenny.tistory.com/387
https://velog.io/@rosa/Lombok-%EC%A7%80%EC%96%91%ED%95%B4%EC%95%BC-%ED%95%A0-annotation
'TIL > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
thymeleaf layout ์ ์ฉํ๊ธฐ (header, footer, main) (0) | 2024.01.26 |
---|---|
jre jdk ์ฐจ์ด (0) | 2024.01.18 |
java ์ค๋ฒ๋ก๋ฉ๊ณผ ์ค๋ฒ๋ผ์ด๋ฉ (0) | 2024.01.13 |
Spring , RequestParam๊ณผ PathVariable ๊ตฌ๋ถ (1) | 2024.01.12 |
java ์๋ฃ๊ตฌ์กฐ (1) | 2024.01.04 |