스프링

[spring]스프링 빈

삶은겨란 2022. 3. 2. 20:58

컨트롤러가 서비스와 리포지토리를 사용할 수 있게 의존관계 준비.

 

 

컴포넌트 스캔, 자동 의존관계 설정

<컨트롤러에 의존관계 추가>

@Autowired

스프링이 연관된 객체를 스프링 컨테이너에서 찾아 넣어줌

더보기

DI(의존성 주입)

객체 의존관계를 외부에서 넣어주는 것

 

<서비스 스프링 빈 등록>

@Service

@Autowired

 

<리포지토리 스프링 빈 등록>

@Repository

 

Controller->Service->Repository

스프링 빈을 등록할 때 싱글톤으로 등록=같은 스프링 빈이면 모두 같은 인스턴스

 

자바 코드로 직접 빈 등록

SpringConfig 파일 생성

@Configuration
public class SpringConfig {

    @Bean
    public MemberService memberService() {
        return new MemberService(memberRepository());
    }

    @Bean
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }
}

 

 

'스프링' 카테고리의 다른 글

[spring] 프로젝트 생성  (0) 2022.06.14
[spring]스프링 DB 기술  (0) 2022.04.11
[spring]테스트 케이스 작성  (0) 2022.02.28
[spring]간단한 회원 관리  (0) 2022.02.28
[spring]웹 개발 기초  (0) 2022.02.28