화면 보여주기 역할 -> Controller & View
Controller : MemberService를 통해 회원가입 및 데이터 조회 가능
spring container 에 등록해서 사용
생성자 만드는 방법 : 생성자에 + @Autorwired
@Autowired : spring이 spring container에 있는 memberService를 가져다가 연결
-> 즉, Controller랑 Service랑 연결시켜주기 위해 Autowired를 사용
-> Autowired는 Controller와 MemberService에 @Autowired annotation 입력
Controller를 통해서 외부 요청 받고, Service에서 비즈니스 로직 만들고, Repository 에서 데이터 저장
@Controller
public class MemberController {
private final MemberService memberService;
@Autowired
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
}
@Service
public class MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberService(MemoryMemberRepository memberRepository){
this.memberRepository = memberRepository;
}
...
}
@Repository
public class MemoryMemberRepository implements MemberRepository {
...
}
해당 실행 결과는 HelloSpringApplication에서 run
1) Controller는 기존에 컴포넌트 스캔방식 그대로 사용
@Controller
public class MemberController {
private final MemberService memberService;
@Autowired
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
}
2) Service와 Repository는 직접 스프링 빈에 등록
package hello.hellospring;
import hello.hellospring.repository.MemberRepository;
import hello.hellospring.repository.MemoryMemberRepository;
import hello.hellospring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Member;
@Configuration
public class SpringConfig {
@Bean
public MemberService memberService() {
return new MemberService(memberRepository()); // MemberService를 spring bean에 등록
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
}