코딩기록 저장소 🐕/spring(JPA)🌱

Spring core 할인정책개발 스프링 적용! (김영한의 스프링)

kimkongmom 2023. 9. 20. 10:01

 

 

 

 

스프링으로 전환하기

 

 

 AppConfig 스프링 기반으로 변경

AppConfig에 설정을 구성한다는 뜻의 @Configuration 을 붙여준다.
각 메서드에 @Bean 을 붙여준다. 이렇게 하면 스프링 컨테이너에 스프링 빈으로 등록한다

 

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
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
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 RateDiscountPolicy();
 }
}
cs

 

 

 

 

 MemberApp에 스프링 컨테이너 적용

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package hello.core;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MemberApp {

 public static void main(String[] args) {

// AppConfig appConfig = new AppConfig();
// MemberService memberService = appConfig.memberService();
 ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(AppConfig.class);
 MemberService memberService =
applicationContext.getBean("memberService", MemberService.class);
 Member member = new Member(1L, "memberA", Grade.VIP);
 memberService.join(member);
 Member findMember = memberService.findMember(1L);
 System.out.println("new member = " + member.getName());
 System.out.println("find Member = " + findMember.getName());
 }
}
cs

 

 

 

OrderApp에 스프링 컨테이너 적용

 

 

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
package hello.core;
import hello.core.member.Grade;
import hello.core.member.Member;
import hello.core.member.MemberService;
import hello.core.order.Order;
import hello.core.order.OrderService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class OrderApp {
 public static void main(String[] args) {
// AppConfig appConfig = new AppConfig();
// MemberService memberService = appConfig.memberService();
// OrderService orderService = appConfig.orderService();
 ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(AppConfig.class);
 MemberService memberService =
applicationContext.getBean("memberService", MemberService.class);
 OrderService orderService = applicationContext.getBean("orderService"
OrderService.class);
 long memberId = 1L;
 Member member = new Member(memberId, "memberA", Grade.VIP);
 memberService.join(member);
 Order order = orderService.createOrder(memberId, "itemA"10000);
 System.out.println("order = " + order);
 }
}
cs

 

 

 

 

▶ 스프링 컨테이너

 

⚫︎ ApplicationContext 를 스프링 컨테이너라 한다.

⚫︎기존에는 개발자가 AppConfig 를 사용해서 직접 객체를 생성하고 DI를 했지만,

   이제부터는 스프링 컨테이너를 통해서 사용한다.

⚫︎스프링 컨테이너는 @Configuration 이 붙은 AppConfig 를 설정(구성) 정보로 사용한다.

⚫︎여기서 @Bean 이 라 적힌 메서드를 모두 호출해서 반환된 객체를 스프링 컨테이너에 등록한다.

⚫︎이렇게 스프링 컨테이너에 등록된 객체를 스프링 빈이라 한다.

⚫︎스프링 빈은 @Bean 이 붙은 메서드의 명을 스프링 빈의 이름으로 사용한다.( memberService , orderService )

⚫︎이전에는 개발자가 필요한 객체를 AppConfig 를 사용해서 직접 조회했지만,

   이제부터는 스프링 컨테이너를 통 해서 필요한 스프링 빈(객체)를 찾아야 한다.

⚫︎스프링 빈은 applicationContext.getBean() 메서드를 사용 해서 찾을 수 있다.

⚫︎기존에는 개발자가 직접 자바코드로 모든 것을 했다면 이제부터는 스프링 컨테이너에 객체를 스프링 빈으로 등록 하고,

   스프링 컨테이너에서 스프링 빈을 찾아서 사용하도록 변경되었다.