🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

DI 와 전반적인 구조 질문

24.05.24 12:42 작성 조회수 74

0

[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오) 네
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오) 네
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오) 네

[질문 내용]
1. 회원 서비스를 테스트할 때, DI를 하는 이유를 저는 아래처럼 이해했는데 이것이 맞는 이해인지 궁금합니다.

  • MemberService() 를 실행하는 순간 MemoryMemberRepository가 new로 생성되고, 그 다음줄에서 MemoryMemberRepository를 새롭개 new로 생성한다.

  • 현재의 코드에서는 Storestatic하게 선언되었으므로 그런 문제가 발생하지 않겠지만, 그렇지 않다면 MemberServiceMemoryMemberRepository가 서로 다른 레포지토리 인스턴스를 가지게된다.

  1. 위 내용이 맞다면, 모듈 테스트를 하지 않는다는 가정하에 해당 부분 뒤에 나오는 스프링 통합 테스트를 할 때는 이미 스프링 컨테이너에 레포지토리가 등록되어 있으므로 생성자를 활용한 의존성 주입이 필요없는 것이 맞나요?

 

  1. 아래 그림은 최종적으로 제가 구조를 이해한 것을 구조화 한 것인데 혹시 틀린 부분이 있는지 궁금합니다.

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.05.25

안녕하세요, 인프런 AI 인턴입니다.

  1. 네, 귀하의 이해는 맞습니다. 의존성 주입(DI)을 통해 MemberServiceMemoryMemberRepository가 동일한 인스턴스를 참조하도록 하여 일관성 있는 데이터 활용이 가능해지며, 또한 이는 테스트 용이성을 향상시킵니다.

  2. 스프링 통합 테스트에서는 스프링 컨테이너를 사용하여 의존성을 관리합니다. 즉, 스프링 컨테이너가 자동으로 의존성을 주입해주기 때문에 개발자가 직접 생성자를 통해 의존성을 주입할 필요가 없습니다. 그러므로 여러분의 이해는 옳습니다.

  3. 제공해주신 다이어그램은 스프링 프레임워크 응용 프로그램의 전형적인 구조를 잘 나타내고 있습니다. 도메인, 서비스, 리포지토리 및 컨트롤러 간의 관계와 스프링 컨테이너에 의한 의존성 주입 방식을 정확하게 설명하고 있습니다. @Autowired를 사용한 의존성 주입과 각 계층(Controller, Service, Repository)의 역할도 적절히 표현되어 있습니다. 특별히 지적할 오류는 보이지 않습니다.

This image appears to be a diagram representing the structure of a Spring Framework application in the context of Java development. The diagram is labeled in Korean and depicts different components of a typical Spring application and how they are interconnected. Here’s an explanation of the elements in the diagram:1. 스프링 컨테이너 (IoC Container): This translates to “Spring Container (IoC Container),” which is referring to the Inversion of Control container provided by the Spring Framework that manages the lifecycle and configuration of the application beans.2. 컨트롤러 (Controller): Refers to the Controller component in a Spring MVC (Model-View-Controller) application. It’s annotated with `@Controller`, indicating that it’s a Spring MVC controller.3. 서비스 (Service): Stands for the Service layer, which contains business logic. It’s annotated with `@Service` or could be defined in a configuration with `@Bean`.4. 리포지토리 (Repository): This is the Repository layer, responsible for data access operations, and is annotated with `@Repository` or again, alternatively defined in a configuration with `@Bean`.The `@Autowired` annotations indicate where Spring should perform dependency injection, automatically wiring beans together.5. 도메인 (Domain): Represents the Domain layer, which includes entities and domain logic in the application.6. DB: Represents the database with which the application interacts.The rectangles (domain, service, repository) seem to correspond to the layers of the application, while the arrows represent the flow of control or dependencies.Please note that the explanations are also annotated with terms like `데이터`, `Jdbc`, `JPA 등`, which refer to data, JDBC (Java Database Connectivity), JPA (Java Persistence API), indicating technologies that might be used in the repository layer for data access in the database.This diagram provides an overview of beans and their relationships often used in object-oriented programming with the Spring framework, focusing on the use of annotations (`@Controller`, `@Service`, `@Repository`, `@Autowired`) to declare components and manage dependency injection.

채널톡 아이콘