교육과정 기록/💻back-end

Spring 의존성 주입 (DI)

춘식이왔엉 2022. 8. 10. 21:58

 

 

< 실습 > 

- 스프링 프로젝트에, 의존성을 주입할 클래스 3개 생성

1. Restaurant 클래스

@NoArgsConstructor

@Component("restaurant")
public class Restaurant {
	;;
} // class

 

2. Chef 클래스

@NoArgsConstructor		// 모든 생성자는 명시적으로 보이게 하기

@Component("Chef")		// stereotype annotations
public class Chef {
	;;
} // class

 

3. Hotel 클래스

@NoArgsConstructor

@Component("Hotel")
public class Hotel {	
	;;
} // class

 

- src 폴더 안에서 root-context.xml 파일 찾아 열기

 

1) 자동으로 자바빈즈 클래스를 Spring Beans Container (== Spring Context)에 빈(Bean) 등록하기- 스프링 프로젝트 root-context.xml 파일에서 Namespaces 클릭 후 context 체크 -

 

- xml 파일에 xmlns:context 부분 자동 생성됨

- <context:component-scan> 태그 등록

 

- root-context.xml 파일에서 Beans Graph 클릭 시, 의존성 주입할 콩 3개 등장

 

 

2) 수동으로 자바빈즈 클래스를 Spring Beans Container (== Spring Context)에 빈(Bean) 등록하기

- 스프링 프로젝트의 root-context.xml 파일에서, <beans> 태그 안에 <bean>태그를 이용해 등록 

- 각각의 빈즈 클래스에서  @Component 어노테이션 불필요 !!!

<bean id="" class="">
	<property name="" value="" />
</bean> -->