N° 01 — 목록
'ApplicationContext' (2)
-
Backend Development/SpringSpring - 02. container bean lifecycle
Bean이 생성되고 사라지기까지 — 컨테이너와 생명주기의 모든 단계@PostConstruct 메서드 안에서 null을 역참조해 NullPointerException이 났다. 의존성은 분명 @Autowired로 주입해뒀는데. 원인은 — 그 의존 bean의 @PostConstruct가 아직 실행되지 않았기 때문이다. 생성자에서 같은 의존성을 호출했다면 더 일찍, 더 기이하게 터졌을 것이다.이런 문제는 생명주기(lifecycle) 없이는 풀 수 없다. bean이 인스턴스화에서 소멸까지 거치는 단계 — 생성자 → 의존성 주입 → @PostConstruct → BeanPostProcessor 후처리 → 사용 → @PreDestroy — 의 어느 시점에 무엇이 보장되고 무엇이 보장되지 않는지를 알아야, "왜 여기서..
-
Backend Development/SpringSpring - 01. IoC DI
new를 직접 부르는 코드가 테스트하기 어려운 이유 — IoC와 DI가 해결하는 문제// Spring 없는 세계public class OrderService { private final PaymentGateway gateway = new StripePaymentGateway(); // 직접 생성 private final MailSender mailer = new SmtpMailSender(); // 직접 생성 public void process(Order order) { gateway.charge(order.getTotal()); mailer.send(order.getCustomerEmail(), "주문 완료"); }}이 코드는 ..