저번 글에서 궁금한 점이나 추가 설명이 필요한 부분들을 다룰 것이다.
Spring은 객체지향을 하는 프레임워크다. 웹을 하는 프레임워크는 아니다.
웹으로 확장이 가능한 것이다.
그래서 servlet-context.xml은 웹과 관련된 설정을 하고
root-context.xml은 객체지향을 관리하는 xml로 나누어 관리하는 것이다.
xml을 둘로 나눈 이유다.
만약 에러가 나면 어디부터 확인해야하는가?
바로 첫번째는 컴포넌트 스캔이 되어있는지 확인을 하는 것이다.
이것이 무슨 이야기인지 확인해보자.
SampleHotel이라는 클래스를 만들어서 확인해보겠다.
package org.zero.sample;
import org.springframework.stereotype.Component;
@Component
//component는 얘는 Spring에서 내가 만들어서 알고 관리를 해주어야하는 구나 라고 생각함
public class SampleHotel {
}
이렇게 component-scan에 생기게 된다.
요런식으로 bean이 등록이 된것이다.
그러면 호텔에 쉐프 레스토랑이 들어온다. 그것을 연습해보자
Alt+Shift+s 하고 construct using filed를 클릭하고 chef를 추가하면 다음의 코드가 추가된다.
public SampleHotel(Chef chef) {
super();
this.chef = chef;
}
그리고 이제 다시 SampleTests로가서 확인을 해보자
package org.zero.sample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import lombok.extern.log4j.Log4j;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class SampleTests {
@Autowired
private Restaurant restaurant;
@Autowired
private SampleHotel hotel;
@Test
public void testHotel() {
log.info(hotel);
}
@Test
public void test1() {
System.out.println("test!!!!!!!!!!");
log.info("Testseeeeeeeeeeeee");
log.info(restaurant);
}
}
testHotel 메서드를 실행시켜보면
chef가 나오게된다. 내가 한일은 생성자만 만들어준거 뿐인데 신기하다...
그런데 이거도 안하는 것이 저번글에서 설명한 final 단일 생성자 주입이다.
'Spring' 카테고리의 다른 글
[Part 1] HikariCP 설정 #7 (0) | 2023.01.12 |
---|---|
[Part 1] DB연결하기 #6 (0) | 2023.01.10 |
[Part 1] 의존성 주입 실습 #4 (0) | 2023.01.08 |
[Part 1] 프로젝트 생성 #3 (0) | 2023.01.08 |
[Part 1] 프로젝트 생성 #2 (0) | 2023.01.07 |