스프링 핵심 원리 - 기본편(빈 생명주기 콜백)
·
Spring
애플리케이션 종료 시점에 연결을 모두 종료하기 위해 객체의 초기화와 종료 작업이 필요하다 스프링 빈은 객체를 생성한 다음 의존관계를 주입하는 라이프사이클을 가진다. (ex: Setter, Field Injection) 예외) 생성자 주입 초기화 작업은 의존관계 세팅이 완료된 후에 진행된다. 그 시점을 어떻게 알까? 👉스프링이 초기화 콜백으로 알려준다 ^^ 또한 스프링 컨테이너는 종료되기 직전에 소멸 콜백을 준다. 스프링 컨테이너 생성 - 스프링 빈 생성 - 의존관계 주입 - 초기화 콜백 - 사용 - 소멸전 콜백 - 스프링 종료 초기화 콜백 : 빈이 생성되고, 빈의 의존관계 주입이 완료된 후 호출 소멸전 콜백 : 빈이 소멸되기 직전에 호출 객체의 생성과 초기화를 분리하자 객체의 생성 : 메모리를 할당해 객..
스프링 핵심 원리 - 기본편(의존관계 자동 주입)
·
Spring
[스프링 의존관계 주입 방법] 1. 생성자를 통한 의존관계 주입 -> 주로 불변, 필수 의존관계에 사용된다. @Autowired public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) { System.out.println("memberRepository = " + memberRepository); System.out.println("discountPolicy = " + discountPolicy); this.memberRepository = memberRepository; this.discountPolicy = discountPolicy; } - 생성자 호출시점에 딱 한번만 호출되는것이 보장된다. *..
스프링 핵심 원리 - 기본편(컴포넌트 스캔과 의존관계 자동 주입)
·
Spring
스프링은 설정 정보가 없어도 자동으로 스프링 빈을 등록하는 @ComponentScan이라는 기능을 제공한다. 또한 의존관계도 자동으로 주입하는 @Autorwired라는 기능을 제공한다. 생성자에 @Autowired를 넣어 의존관계를 주입해보자💨 @Component public class MemberServiceImpl implements MemberService{ private MemberRepository memberRepository; @Autowired public MemberServiceImpl(MemberRepository memberRepository) { this.memberRepository = memberRepository; } [ComponentScan] 스프링 컨테이너가 @Compon..
스프링 핵심 원리(기본편) - 스프링 컨테이너와 스프링 빈
·
Spring
* "AnnotationConfigApplicationContext".getBeanDefinitionNames() : 스프링에 등록된 모든 빈 네임을 조회 * "AnnotationConfigApplicationContext".getBean(빈 이름 , [타입]) : 빈 이름으로 객체 조회 # 스프링 빈 조회 시 같은 타입이 둘 이상이면 에러가 발생한다. -> 네임으로 조회하기! import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; // static으로 import 해준다 AnnotationConfigApplicationContext ac = new..
스프링 핵심 원리(기본편) - 객체 지향 원리 적용
·
Spring
AppConfig : IoC Conatiner / DI container -> 객체를 생성하고 관리하며 의존관계를 주입해준다 //AppConfig.java @Configuration public class AppConfig { @Bean public MemberService memberService() { return new MemberServiceImpl(memberRepository()); } @Bean public OrderService orderService() { return new OrderServiceImpl(memberRepository(), discountPolicy()); } @Bean public DiscountPolicy discountPolicy(){ // return new Fi..