인프런 커뮤니티 질문&답변

민혁님의 프로필 이미지
민혁

작성한 질문수

스프링 핵심 원리 - 기본편

관심사의 분리

질문 있습니다!

해결된 질문

작성

·

265

0

private static Map<Long, Member> store = new HashMap<>();

이 부분도 관심사 분리?를 해보고 싶어 아래와 같이 코드를 작성했는데 실행은 정상적으로 됐습니다.

 

질문 1. 코드가 제가 의도한 것처럼 짜여진게 맞는지 궁금합니다

질문 2. 만약 맞다면 코드가 좀 지저분해보이는데.. 더 좋은 방법이 있는지 궁금합니다

 

좋은 강의 제공해주신 영한님과 고생하시는 서포터즈분들 정말 감사합니다!!

package hello.core.member;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MemoryMemberRepository implements MemberRepository {

    private static Map<Long, Member> store;

    @Autowired
    public MemoryMemberRepository(Map<Long, Member> store) {
        this.store = store;
    }

    @Autowired
    public MemoryMemberRepository() {
    }

    @Override
    public void save(Member member) {
        store.put(member.getId(), member);
    }

    @Override
    public Member findById(Long memberId) {
        return store.get(memberId);
    }
}
package hello.core;

import hello.core.member.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MemberApp {

    public static void main(String[] args) {

        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        MemoryMemberRepository memorymemberRepository = ac.getBean("memorymemberRepository", MemoryMemberRepository.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());

    }
}
package hello.core;

import hello.core.discount.DiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.*;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class AppConfig {

    @Bean
    public MemoryMemberRepository memorymemberRepository() {
        return new MemoryMemberRepository(new HashMap<Long, Member>());
    }

    @Bean
    public MemberService memberService() {
        System.out.println("call AppConfig.memberService");
        return new MemberServiceImpl(memberRepository());
    }

    @Bean
    public MemberRepository memberRepository() {
        System.out.println("call AppConfig.memberRepository");
        return new MemoryMemberRepository();
    }

    @Bean
    public OrderService orderService() {
        System.out.println("call AppConfig.orderService");
        return new OrderServiceImpl(memberRepository(), discountPolicy());
    }

    @Bean
    public DiscountPolicy discountPolicy() {
        return new RateDiscountPolicy();

    }
}

 

답변 3

1

안녕하세요. 민혁님, 공식 서포터즈 David입니다.

의도하신대로 작성하시려면 HashMap을 생성하는 것도 memberRepository()와 같이 별도의 메서드로 빼면 좋을 것 같습니다.

감사합니다.

0

민혁님의 프로필 이미지
민혁
질문자

바쁘실텐데 답변해주셔서 감사합니다!!

0

민혁님의 프로필 이미지
민혁
질문자

 

자꾸 질문드려 죄송합니다.. 혹시 밑처럼 작성하면 될까요??

package hello.core;

import hello.core.discount.DiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.*;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class AppConfig {

    @Bean
    public MemoryMemberRepository memorymemberRepository() {
        return new MemoryMemberRepository(getMap());
    }

    @Bean
    public HashMap<Long, Member> getMap() {
        return new HashMap<Long, Member>();
    }

    @Bean
    public MemberService memberService() {
        System.out.println("call AppConfig.memberService");
        return new MemberServiceImpl(memberRepository());
    }

    @Bean
    public MemberRepository memberRepository() {
        System.out.println("call AppConfig.memberRepository");
        return new MemoryMemberRepository();
    }

    @Bean
    public OrderService orderService() {
        System.out.println("call AppConfig.orderService");
        return new OrderServiceImpl(memberRepository(), discountPolicy());
    }

    @Bean
    public DiscountPolicy discountPolicy() {
        return new RateDiscountPolicy();

    }
}

굳이 빈으로 등록하실 필요는 없습니다:)

민혁님의 프로필 이미지
민혁

작성한 질문수

질문하기