강의

멘토링

커뮤니티

Inflearn Community Q&A

yozzangga's profile image
yozzangga

asked

Spring Framework Core Technologies

Java Bean 설정 파일 내부 DI 관련 문의

Written on

·

318

0

안녕하세요 기선님,

좋은 강의 열심히 듣고 있는데요, 강의 내용 중에 궁금한 점이 하나 있어서 문의드립니다.

Java Bean 설정 파일 내부에서 의존성 주입을 받는 시나리오 중에 아래와 같이

public class ApplicationConfig {
@Bean
public BookService bookService() {
BookService bookService = new BookService();
//bookService.setBookRepository(bookRepository);
bookService.setBookRepository(bookRepository());
return bookService;
}

@Bean BookRepository bookRepository() {
return new BookRepository();
}
}

@Bean 메서드를 직접 호출해서 주입을 받으면 Bean으로 등록된 객체가 전달되지 않고 새로운 객체를 생성하는 것 같습니다.

<context.getBean("bookRepository")와 context.getBean("bookService").getBookRepository() 출력결과>

com.sboo.springframeworktutorial.BookRepository@50ad3bc1

com.sboo.springframeworktutorial.BookRepository@223aa2f7

제가 잘못 프로그래밍한 부분이 있을까요?

늘 감사드립니다~

springjava

Answer 2

1

whiteship님의 프로필 이미지
whiteship
Instructor

빈 설정으로 사용할 클래스 위에 @Configuration을 선언해 주세요.

0

yozzangga님의 프로필 이미지
yozzangga
Questioner

아..이렇게 사용하는 경우 어노테이션을 붙여야 정상적으로 주입이 되는군요.

답변 감사드립니다!!

yozzangga's profile image
yozzangga

asked

Ask a question