First Spring

마동찬·2023년 4월 18일
0

@RequiredArgsConstructor

  • 의존성을 주입해주기 위해서 생성자(Constructor), Setter, Field 타입의 방식을 사용해야 했다. 하지만 lombok(롬복)의 @RequiredArgsConstructor 어노테이션을 사용하면 간단한 방법으로 생성자 주입을 해줄 수 있다.

  • @RequiredArgsConstructor는 final 혹은 @NotNull이 붙은 필드의 생성자를 자동으로 만들어준다. 이를 통해 새로운 필드를 추가할 때 다시 생성자를 만들거나 하는 등의 번거로움을 없앨 수 있다.

@Service
@RequiredArgsConstructor
public class TestService {
    private final TestRepository1 testRepository1;
    private final TestRepository2 testRepository2;
}

위아래 차이비교해보기

@Service
public class TestService {
    private final TestRepository1 testRepository1;
    private final TestRepository2 testRepository2;

    @Autowired
    public TestService(TestRepository1 testRepository1, TestRepository2 testRepository2) {
        this.testRepository1 = testRepository1;
        this.testRepository2 = testRepository2;

    }
}

isPresent()

  • Boolean 타입

  • Optional 객체가 값을 가지고 있다면 true, 값이 없다면 false 리턴

ifPresent()

  • Void 타입

  • ifPresent()는 Optional 객체가 값을 가지고 있으면 실행 값이 없으면 넘어감

userRepository.findById(id).ifPresent(a -> {
	throw new BadRequestExeption
})
profile
새내기개발자 성장기록

0개의 댓글