본문 바로가기
Spring

[Spring] @PostConstruct 활용한 초기 설정

by Apère 2023. 2. 27.
반응형

@PostConstruct는 다음과 같은 특징이 있습니다

 

  • 의존성 주입이 완료된 후에 실행되어야 하는 method에 사용
  • 해당 어노테이션은 다른 리소스에서 호출되지 않아도 수행
  • 생성자 보다 늦게 호출된다

때문에 아래 코드처럼 application이 처음 부팅되는 순간 초기 설정이 필요한경우 이용합니다

 

@Component
@RequiredArgsConstructor
public class PostConstructBean {

    private final TestService testService;

    @PostConstruct
    public void init() throws Exception {
    	testService.init();
    }
}
반응형

댓글