• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

독립 실행형 스프링 애플리케이션까지 본 수강생입니다. 질문있습니다.

23.08.31 14:40 작성 23.08.31 14:42 수정 조회수 302

0

질문은 다음과 같습니다...!

====

Spring Container 제작시, 자기자신을 Bean으로 등록하는 이유

====

 

코드를 수정했습니다.

  1. Servlet Container, DispatcherServlet을 Bean으로 등록하는 코드를 config 패키지의 클래스로 따로 빼버렸습니다.

    [ :이유:

    ComponentScan은 자기자신을 탐색범위에 넣지 않는다

    ->클래스 내부의 Configuration을 찾지 못한다

    -> Configuration만 외부로 빼면 자기자신을 Bean으로 등록하지 않아도 된다

    ]

  2.  

  3. Spring Container를 만드는 과정에 Servlet Container를 만들지 않습니다. 분리시켰습니다.

    (순서대로 만들어진다는 느낌을 받기위해...)

 

다음은 제가 작성한 main 문의 코드입니다.

@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
       //Spring Container 구성정보
       AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
       // Bean 등록
       applicationContext.register(DemoApplication.class);
       //Spring Container 실행(초기화)
       applicationContext.refresh();

//     =========================================
       //Servlet Container 구성정보
       ServletWebServerFactory serverFactory = applicationContext.getBean(ServletWebServerFactory.class);
       //servlet 만들기
       DispatcherServlet dispatcherServlet = applicationContext.getBean(DispatcherServlet.class);
       dispatcherServlet.setApplicationContext(applicationContext);
       //Servlet Container 인스턴스
       WebServer webServer = serverFactory.getWebServer(servletContext -> {
          servletContext.addServlet("dispathcerServlet",
                dispatcherServlet).addMapping("/*");
       });
       //서버 실행
       webServer.start();
    }
}


1번의 이유로,

// Bean 등록
       applicationContext.register(DemoApplication.class);

이걸 주석처리했습니다.

같은 클래스내에 ApplicationContext가 있고, 생성도 완료한 시점이기 때문에, 본인이 Bean에 직접 접근해서 꺼내 써도 된다고 생각했습니다.

실제로도 직접 Bean을 꺼내서 주입시켜버립니다.

   ServletWebServerFactory serverFactory = applicationContext.getBean(ServletWebServerFactory.class);
       DispatcherServlet dispatcherServlet = applicationContext.getBean(DispatcherServlet.class);
       

따라서 저는, Spring Container가
아래 2줄의 코드에서 Bean을 다 생성하고 DI까지 완료한줄 알았습니다.

//Spring Container 구성정보
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
// Bean 등록
//applicationContext.register(DemoApplication.class);
//Spring Container 실행(초기화)
applicationContext.refresh();

어차피 서블릿 환경 설정도 저 어딘가에서 ComponentScan 해서 Bean으로 등록하기 때문에

자기자신을 Bean으로 등록하는건 필요 없는줄 알았습니다.

 

그리고 발생한 에러 입니다.

No qualifying bean of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' available

 

 

=======
주석만 풀면 잘 돌아갑니다. 왜이럴까요...?

 

답변 1

답변을 작성해보세요.

0

가능하면 제가 강의에 구성한 순서를 따라서 진행해보시는 걸 권장드립니다.

그래도 시도해보셨으니 몇 가지 생각을 말씀드리면,
@ComponentScan이 동작하려면 그게 붙어있는 클래스가 스프링 빈으로 미리 등록이 되어야 합니다. 그냥 애노테이션 붙인다고 저절로 스캔이 되지 않습니다. 아무 클래스에 @ComponentScan을 붙인다고 알아서 스캔을 하지 않습니다.

그런데 applicationContext.register(DemoApplication.class); 를 주석처리하셨으니 DemoApplication클래스는 빈이 될 기회가 없으니 당연히 다른 빈들을 못 찾는다고 에러가 나겠죠.
이렇게 코드로 application context를 만드는 경우 시작 시점이 되는 클래스를 register로 등록하는 것은 "필수" 입니다. 이걸 생략하고는 어떤 방식으로도 스프링의 구성이 바르게 될 수 없습니다.

다시 말씀드리지만 다양한 시도는 일단 강의를 끝까지 다 보시고 나서 해보시는 것을 권장드립니다.