스프링 기반으로 변경하기
기존의 DI컨테이너(AppConfig)는 순수한 자바코드로 의존성을 주입했었다. 이제부터는 스프링을 이용하여 의존성을 주입하겠다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package hello.core;
import hello.core.Repository.MemberRepository;
import hello.core.Repository.MemoryMemberRepository;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.memberService.MemberService;
import hello.core.memberService.MemberServiceImpl;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
@Bean
public OrderService orderService() {
return new OrderServiceImpl(
memberRepository(),
discountPolicy());
}
@Bean
public MemberRepository memberRepository(){
return new MemoryMemberRepository();
}
@Bean
public DiscountPolicy discountPolicy() {
return new FixDiscountPolicy();
}
}
|
cs |
스프링은 어노테이션(Annotation) 기반의 프레임워크이다.
@Configuration : 설정을 뜻하는 Configuration 은 단어 그대로 받아들이면 될 것 같다. @Configuration 이 붙은 설정파일 내부에서 @Bean 은 해당 메소드를 전부 반환하여 스프링 컨테이너에 등록해준다.
@Bean : 스프링 컨테이너에 등록된 객체를 스프링 빈이라고 한다. 스프링 빈은 메소드의 명을 스프링 빈의 이름으로 사용한다.
MemberSerivce --> memberService --> 스프링 빈의 이름.
ApplicationContext 란 ?
1
2
3
|
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
MemberService memberService = ac.getBean("memberService", MemberService.class);
OrderService orderService = ac.getBean("orderService", OrderService.class);
|
cs |
ApplicationContext 란 스프링 컨테이너를 뜻한다. 스프링 컨테이너란 @Bean (스프링 빈) 을 담는 컨테이너이다.
DI 컨테이너를 명시해주는 것으로 선언한다. 꺼낼 때는 .getBean 을 통해 스프링 빈의 이름과 , 해당 스프링 빈의 타입을 입력한다.
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplictaionContext 는 위 그림에서 볼 수 있듯이 ApplicationContext 의 상속관계이다.
즉 ApplicationContext 의 구현체이다.
* 부모는 자식을 담을 수 있다.
스프링 빈을 조회
1
2
3
4
5
6
7
8
9
10
11
12
|
class ApplicationContextBasicFindTest {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
@Test
void findBeanByName (){
MemberService memberService = ac.getBean("memberService", MemberService.class);
Assertions.assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
}
}
|
cs |
BeanFactory 와 ApplicationContext
BeanFactory는 스프링 컨테이너의 최상위 인터페이스다. 스프링 빈을 관리하고 조회하는 역할을 담당하며 getBean 기능도 제공한다. 그럼에도 BeanFactory를 쓰지 않고 'ApplicationContext' 를 쓰는 이유는 수 많은 부가기능을 제공해 주기 때문이다.
- 국제화 기능 : 한국에서 들어오면 한국어로, 영어권에서 들어오면 영어로 출력한다.
- 환경변수 , 애플리케이션 이벤트 , 편리한 리소스 조회 등 ...
BeanFactory , ApplicationContext 둘 다 스프링 컨테이너라고 부른다.
BeanDefinition 이란 ?
스프링은 자바 코드 뿐만이 아니라 xml 그 이외에도 다양한 언어를 지원하여 스프링 빈으로 등록할 수 있게 도와준다.
BeanDefinition 이 이러한 역할을 해준다는 것을 기억하고 넘어가자.
- 다음 장에서는 싱글톤에 대해 알아본다.
* 해당 글은 'Infleran'의 김영한 강사님의 자료를 참조하였습니다.
'Spring > Spring Framework(Basic)' 카테고리의 다른 글
[Spring] Spring 을 이용하여 Service 구축하기 - 3 (0) | 2021.10.29 |
---|---|
[Spring] Spring 을 이용하여 Service 구축하기 - 2 (0) | 2021.10.28 |
[Spring] 순수 자바 코드를 이용해 Service 구축하기 - 3 (0) | 2021.10.28 |
[Spring] 순수 자바 코드를 이용해 Service 구축하기 - 2 (0) | 2021.10.27 |
[Spring] 순수 자바 코드를 이용해 Service 구축하기 - 1 (0) | 2021.10.26 |
댓글