• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

THIS!

23.03.14 22:14 작성 조회수 395

1

안녕하세요, 자바 기본 적인 질문 한가지만 드릴게요...!

 

public class HellobootApplication {

public static void main(String[] args) {

GenericWebApplicationContext applicationContext = new GenericWebApplicationContext() {

@Override

protected void onRefresh() {

super.onRefresh();

ServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();

WebServer webServer = serverFactory.getWebServer(servletContext -> {

servletContext.addServlet("dispatcherServlet",

new DispatcherServlet(this))

.addMapping("/*");

});

webServer.start();

}

};

applicationContext.registerBean(HelloController.class);

applicationContext.registerBean(SimpleHelloService.class);

applicationContext.refresh();

}

}

 

여기서 this는 HellobootApplication class가 아닌, applicationContext 를 지칭 하는게 맞을까요..?

매번 this가 조금씩 헷갈리네요 ,,,,

이상한 질문 죄송합니다....!

답변 1

답변을 작성해보세요.

1

익명 클래스 내부에서 쓰는 this는 익명 클래스에 의해서 만들어지는 오브젝트를 가리킵니다.
따라서 위 코드에서 this는 다음 익명 클래스 내부에서 사용되므로 GenericWebApplicationContext 타입의 오브젝트가 됩니다.

new GenericWebApplicationContext() {
}