코드 그라데이션

이제! 스프링으로 전환하기 본문

Spring/핵심 원리 구현

이제! 스프링으로 전환하기

완벽한 장면 2024. 1. 20. 02:33

스프링으로 전환하기

지금까지 순수한 자바 코드만으로 DI를 적용했다. 이제 스프링을 사용한다.

 

 

AppConfig 스프링 기반으로 변경

package inflearn.spring_core.config;

import inflearn.spring_core.discount.DiscountPolicy;
import inflearn.spring_core.discount.RateDiscountPolicy;
import inflearn.spring_core.member.MemberRepository;
import inflearn.spring_core.member.MemberService;
import inflearn.spring_core.member.MemberServiceImpl;
import inflearn.spring_core.member.MemoryMemberRepository;
import inflearn.spring_core.order.OrderService;
import inflearn.spring_core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 애플리케이션의 전체 동작 방식을 구성(config)하기 위해, 구현 객체를 생성하고, 연결하는 책임을 가지는 별도의 설정 클래스
@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();
    }
}

 

MemberApp에 스프링 컨테이너 적용

package inflearn.spring_core;

import inflearn.spring_core.config.AppConfig;
import inflearn.spring_core.member.Grade;
import inflearn.spring_core.member.Member;
import inflearn.spring_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();//직접 객체생성하던걸
//        AppConfig appConfig = new AppConfig(); // 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());
    }
}

 

"memberService" 이름

MemberService.class 타입

이렇게 등록하면 관련된 것들이 등록이 된다.

 

OrderApp 도 마찬가지 원리. 

 

OrderApp에 스프링 컨테이너 적용

package inflearn.spring_core;

import inflearn.spring_core.config.AppConfig;
import inflearn.spring_core.member.Grade;
import inflearn.spring_core.member.Member;
import inflearn.spring_core.member.MemberService;
import inflearn.spring_core.order.Order;
import inflearn.spring_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);
    }
}
  • 두 코드를 실행하면 스프링 관련 로그가 몇줄 실행되면서 기존과 동일한 결과가 출력된다.

 

 

 

 

어떤 장점이 있을지 생각해보자...!!!

 

728x90
Comments