어플리케이션의 구성정보를 담당하는 파일에 몇개의 어노테이션을 달아준다.
package Goat.core;
import Goat.core.discount.DiscountPolicy;
import Goat.core.discount.RateDiscountPolicy;
import Goat.core.member.MemberRepository;
import Goat.core.member.MemberService;
import Goat.core.member.MemberServiceImpl;
import Goat.core.member.MemoryMemberRepository;
import Goat.core.order.OrderService;
import Goat.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
private static MemberRepository memberRepository() { //굳이 public으로 ?
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService(){
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
@Bean
private static DiscountPolicy discountPolicy() {
// return new FixDiscountPolicy();
return new RateDiscountPolicy();
}
}
package Goat.core;
import Goat.core.member.Grade;
import Goat.core.member.Member;
import Goat.core.member.MemberService;
import Goat.core.member.MemberServiceImpl;
import Goat.core.order.OrderService;
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);
//스프링 컨테이너 ApplicationContext
MemberService memberService = applicationContext.getBean("memberService", MemberService.class);
//메소드 이름을 이름 등록(첫번째 인자) 두번째인자는 반환 타입
Member member = new Member(1L,"GOAT", Grade.VIP);
memberService.join(member);
Member findMember = memberService.findMember(1L);
System.out.println("findMember = " +findMember.getName());
System.out.println("member = " + member.getName());
}
}
- ApplicationContext
ApplicationContext는 스프링 컨테이너라고 생각하면 된다. AppConfig에 @Configuration 이라는 어노테이션을 달았기 때문에, AnnotationConfigApplicationContext 메소드를 활용하여 파라미터로 AppConfig.class로 넘긴다. 그러면 생성된 객체 applicationContext가 AppConfig에서 Bean으로 등록된 메소드를 관리해준다.
applicationContext (스프링 컨테이너) 에서는 @Bean 이라고 적힌 메서드를 모두 호출해서 반환된 객체들을 해당 스프링 컨테이너에 등록한다. 이러한 객체들을 스프링 빈이라고 한다.- getBean
AppConfig를 관리하는 스프링 컨테이너 객체를 생성했으면, 해당 객체를를 가져 와야 한다.
이를 위해 applicationContext.getBean("메소드 이름", "반환 타입") 을 호출하면 된다. 그러면 스프링 빈에 등록된 스프링 빈들을 가져올 수 있는 것이다.- 실행 결과
@Bean 으로 등록했던 메소드들이 커맨드창에 뜸.
package Goat.core;
import Goat.core.member.Grade;
import Goat.core.member.Member;
import Goat.core.member.MemberService;
import Goat.core.member.MemberServiceImpl;
import Goat.core.order.Order;
import Goat.core.order.OrderService;
import Goat.core.order.OrderServiceImpl;
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,"Goat", Grade.VIP);
memberService.join(member);
Order order = orderService.createOrder(memberId,"itemA",20000);
System.out.println("order = "+order);
}
}