티스토리 뷰
- 드디어 스프링으로 변경해보기
AppConfig.java
package com.hello.core;
import com.hello.core.Order.OrderService;
import com.hello.core.Order.OrderServiceImpl;
import com.hello.core.discount.DiscountPolicy;
import com.hello.core.discount.RateDiscountPolicy;
import com.hello.core.member.MemberRepository;
import com.hello.core.member.MemberService;
import com.hello.core.member.MemberServiceImpl;
import com.hello.core.member.MemoryMemberRepository;
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 MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService(){
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
@Bean
public DiscountPolicy discountPolicy(){
///return new FixDiscountPolicy();
return new RateDiscountPolicy();
}
}
- @Configuration : AppConfig에 설정을 구성한다는 뜻
- @Bean : @Bean이 붙은 메소드들은 스프링 컨테이너에 등록됨
MemberApp.java / 스프링 컨테이너 적용
package com.hello.core;
import com.hello.core.member.Grade;
import com.hello.core.member.Member;
import com.hello.core.member.MemberService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MemberApp {
public static void main(String[] args) {
///MemberService memberService = new MemberServiceImpl();
//Appaonfig 사용
////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());
}
}
OrderApp.java / 스프링 컨테이너 적용
package com.hello.core;
import com.hello.core.Order.Order;
import com.hello.core.Order.OrderService;
import com.hello.core.member.Grade;
import com.hello.core.member.Member;
import com.hello.core.member.MemberService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class OrderApp {
public static void main(String[] args) {
///MemberService memberService = new MemberServiceImpl();
///OrderService orderService = new OrderServiceImpl();
///OrderService orderService = new OrderServiceImpl();
//AppConfig사용
//// 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",20000);
System.out.println("order = " + order);
System.out.println("calculatePrice = " + order.calculatePrice());
}
}
- ApplicationContext : 스프링 컨테이너
- 스프링 컨테이너는 @Configuration이 붙은 AppConfig를 설정 정보로 사용하고 @Bean이 붙은 메소드들을 모두 호출하여 반환된 객체를 스프링 컨테이너에 등록
- .getBean()은 @Bean이 붙은 메소드의 명을 스프링 빈의 이름으로 사용
- 이전에는 필요한 객체를 AppConfig를 사용해 직접 조회했지만 이제부터는 스프링 컨테이너를 통해 applicationContext.getBean() 메소드로 필요한 스프링 빈을 찾음
실행시켜보면 이전과 결과값은 같으나 스프링 어쩌구 크레이팅 어쩌고 싱글톤 빈어쩌고 하는걸 확인할수 있음
'Spring' 카테고리의 다른 글
[Spring] 그냥 간단하게 로깅라이브러리 정리 - SLF4J (0) | 2022.02.08 |
---|---|
[Spring] 간단한 주문 시스템 예제3 - AppConfig로 분리, 리팩토링 (0) | 2021.04.10 |
[Spring] 간단한 주문 시스템 예제2 - '주문','할인' 도메인 개발, 실행, 테스트 (0) | 2021.04.10 |
[Spring] 간단한 주문 시스템 예제 - '회원' 도메인 개발, 실행, 테스트 (0) | 2021.04.10 |
Spring 3 (0) | 2020.10.19 |
댓글
최근에 올라온 글
최근에 달린 댓글
링크
- Total
- Today
- Yesterday