@NoArgsConstructor
인자가 없는
기본 생성자를 생성
@NoArgsConstructor
public class Product {
private String name;
private double price;
}
public Product() {
@AllArgsConstructor
모든 필드
를 인자로 받는 생성자를 생성
- 객체를 생성할 때 모든 필드를 한 번에 초기화 하고싶을때 주로 사용
@AllArgsConstructor
public class Order {
private String orderId;
private String product;
private int quantity;
}
public Order(String orderId, String product, int quantity) {
this.orderId = orderId;
this.product = product;
this.quantity = quantity;
}
@RequiredArgsConstructor
final 필드
나 @NonNull
이 붙은 필드만 인자로 받는 생성자를 생성
- 객체의 불변성을 유지하고자 할 때 주로 사용
- 쉽게 생각하면 객체 초기화를 스프링이 '알아서' 해준다고 보면 될 것 같음
@RequiredArgsConstructor
public class User {
private final String username;
private final String email;
private int age;
public User(String username, String email) {
this.username = username;
this.email = email;