수동 등록 방법
총 4개의 빈을 등록 하였다.
package com.spring.core.chap02.config;
import com.spring.core.chap02.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 스프링 컨테이너에 스프링 빈으 등록하는 설정파일
@Configuration // 이 클래스는 설정 파일 입니다. 를 의미
public class HotelConfig {
// Hotel -> chef, restaurent 의존관계
// Restaurent -> course, restaurent 의존간계
// 빈등록 방식 - 생성자 주입, 세터 주입
@Bean
public Chef chef() {
return new KimuraChef();
}
@Bean
public Course sushiCourse() {
return new SushiCourse();
}
@Bean
public Restaurent restaurent() {
// 생성자 주입(constructor injection)
// EasternRestaurant er = new EasternRestaurant(chef(),sushiCourse());
// 수정자 주입(setter injection)
EasternRestaurant er = new EasternRestaurant();
er.setChef(chef());
er.setCourse(sushiCourse());
// 생성자와 수정자 중 뭐가 더 좋을까?
// 생성자가 더 안정적이다.
// 생성자 주입시 EasternRestaurant 클래스의 Chef와 course가 final일때 수정이 용이함.
return er;
}
@Bean
public Hotel hotel() {
return new Hotel(restaurent(), chef());
}
}
====HotelTest
public interface BeanFactory { // 스프링 컨테이너의 핵심 클래스
}
## 스프링 컨테이너에서 빈을 가져오기
== 별로 중요하지 않은 코드 입니다. ↓ ===
'Spring' 카테고리의 다른 글
★ Spring_기초 스프링 컨테이너와 스프링 빈 (2강)_ 22.07.05(day02) (0) | 2022.07.05 |
---|---|
Spring_ 빈 등록 / 사용 [연습] _ 22.07.05(day02) (0) | 2022.07.05 |
Spring_ XML에 빈 등록 / 사용 _ 22.07.05(day02) (0) | 2022.07.05 |
Spring_설정과 DIP 와 OCP의 이해_예제_ 22.07.04(day01) (0) | 2022.07.04 |
Spring_기초 (1강)_ 22.07.04(day01) (0) | 2022.07.04 |