@PostConstruct
메서드에 지정시, 생성자가 호출된 후에 자동으로 메서드를 호출되게 할 수 있다.
단, @PostConstruct를 사용하려면, 해당 클래스가 빈(Bean)으로 등록되어야 한다.
그렇지 않으면, @PostConstruct가 작동하지 않는다.
Spring이 빈을 생성하고 초기화할 때, 생성자가 호출된 후 @PostConstruct 어노테이션이 붙은 메서드가 호출되어 추가적인 초기화 작업을 수행할 수 있습니다.
@RequestScope
@Component
@Getter
@RequiredArgsConstructor
@Slf4j
public class Rq {
private final HttpServletRequest req;
private final HttpServletResponse resp;
private final MemberService memberService;
private User user;
private Member member;
@PostConstruct
public void init() {
// 현재 로그인한 회원의 인증정보를 가져옴
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.getPrincipal() instanceof User) {
this.user = (User) authentication.getPrincipal();
}
}