Spring Boot

[스프링 입문] 자바 코드로 직접 스프링 빈 등록하기

NHJ 2024. 6. 27. 18:17

앞선 실습에서 붙인 @Service와 @Repository를 삭제하고 자바 코드로 스프링 빈을 등록한다.

 

service 아래에 ServiceConfig 클래스 생성

 

*ServiceConfig

package hello.hello_spring.service;

import hello.hello_spring.repository.MemberRepository;
import hello.hello_spring.repository.MemoryMemberRepository;
import hello.hello_spring.service.MemberService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {
    @Bean
    public MemberService memberService() {
        return new MemberService(memberRepository());
    }
    
    @Bean
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }
}

 

김영한 강사님의 '스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술'을 듣고 정리한 것입니다.