인프런 백기선님의 SpringMVC 강의를 보고 정리한 내용입니다.
DispatcherServlet
이전의 글에서 SpringWebMVC에서는 FrontController로 DispatcherServlet 이라는 것구현해놓았다고 했다.
ServletWebApplicationContext 에서 Controller 설정만 가지고 와서 특정 url에 따른 처리를 해주고
Root WebApplicationContext 에는 Service, Repository 는 모든곳에서 사용할 수 있게 구분을 할 수 있게 하는 구조로 되어있다.
DispatcherServlet 사용하기
위와 같이 Controller는 ServletWebApplicationContext에 등록하고, Service,Repository의 설정은 Root WebApplication에 등록을 해주기
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>me.morris.AppConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>me.morris.WebConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
AppConfig.java : Controller를 제외한 ApplicationContext
WebConfig.java : Controller를 포함한 ApplicationContext
AppConfig.java
package me.morris;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(Controller.class))
public class AppConfig {
}
WebConfig.java
package me.morris;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(useDefaultFilters = false, includeFilters = @ComponentScan.Filter(Controller.class))
public class WebConfig {
}
HelloController.java
package me.morris;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return "Hello, "+ helloService.getName();
}
}
HelloService.java
package me.morris;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String getName(){
return "morrisKim";
}
}
이렇게 RootWebApplicationContext와 Servlet WebapplicattionContext에 나눠서 설정을 할 수 있다.
간단하게 하면 Root WebApplicationContext에 Controller와 Service, Repository 등을 전부 등록하여 사용이 가능하다.
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>me.morris.WebConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
WebConfig.java : ComponentScan을 전체로 등록함
package me.morris;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan
public class WebConfig {
}
'BackEnd > Spring' 카테고리의 다른 글
Eclipse -No tests found with test runner JUnit 5 (1) | 2020.03.10 |
---|---|
@Before, @After - JUnit5 (0) | 2020.03.09 |
SpringWebMVC 시작하기 (2) - Servlet에 IOC 사용하기 (0) | 2020.03.01 |
SpringWebMVC 시작하기 (1)-Servlet (0) | 2020.03.01 |
[Spring] 다른 타입들을 리스트로 받아오기( Jackson Library) (0) | 2020.02.28 |