스프링 컨테이너 생성
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
- ApplicationContext 는 인터페이스이면서 스프링 컨테이너이다.
- 위 코드에서는 AppConfig.class 를 구상 정보로 지정하여 스프링 컨테이너를 생성했다.
스프링 빈 등록
@Configuration
public class AppConfig {
@Bean
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService() {
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
@Bean
public DiscountPolicy discountPolicy() {
return new RateDiscountPolicy();
}
}
- 스프링 컨테이너는 파라미터로 넘어온 설정 클래스 정보를 사용해서 스프링 빈을 등록한다.
- 스프링 빈 이름은 메서드 이름을 사용하며 직접 부여할 수도 있다.
@Bean(name = "memberRepository2")
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
스프링 빈 조회
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
ac.getBean(빈이름, 타입)
ac.getBean(타입)
'WEB > Spring' 카테고리의 다른 글
Web Server, Web Application Server (0) | 2023.05.21 |
---|---|
의존관계 자동 주입 (0) | 2023.05.08 |
DI(Dependency Injection) : 의존관계 주입 (0) | 2023.04.07 |
IoC(Inversion of Control) : 제어의 역전 (0) | 2023.04.06 |
객제 지향 설계의 5가지 원칙(SOLID) (0) | 2023.04.02 |