You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
스프링 빈과 의존관계
컴포넌트 스캔과 자동 의존관계 설정
회원 컨트롤러에 의존 관계 추가
@Autowired가 있으면 스프링이 연관된 객체를 스프링 컨테이너에서 찾아서 넣어준다. 이렇게 객체 의존관계를 외부에서 넣어주는 것을 DI (Dependency Injection), 의존성 주입이라 한다.MemberService에 Bean등록 안하면 아래와 같은 오류가 나옴컴포넌트 스캔 원리
@Component애노테이션이 있으면 스프링 빈으로 자동 등록된다.@Controller컨트롤러가 스프링 빈으로 자동 등록된 이유도 컴포넌트 스캔 때문이다.@Component를 포함하는 다음 애노테이션도 스프링 빈으로 자동 등록된다.@Controller@Service@RepositoryService
@Autowired를 사용하면 객체 생성 시점에 스프링 컨테이너에서 해당 스프링 빈을 찾아서 주입한다. 생성자가 1개만 있으면@Autowired는 생략할 수 있다.Repository
자바 코드로 직접 스프링 빈 등록하기
회원 서비스와 회원 리포지토리의 @service, @repository, @Autowired 애노테이션을 제거하고 진행 한다.
Config 등록
@Autowired를 통한 DI는helloController,memberService등과 같이 스프링이 관리하는 객체에서만 동작한다. 스프링 빈으로 등록하지 않고 내가 직접 생성한 객체에서는 동작하지 않는다.Beta Was this translation helpful? Give feedback.
All reactions