๐
2025-04-29
๐ ํ์ต ๋ด์ฉ
1๏ธโฃ HandlerMapping ๊ฐ์
โ
HandlerMapping์ด๋?
- Spring MVC์์ ํด๋ผ์ด์ธํธ ์์ฒญ์ ๋ฐ์ ์ด๋ค ํธ๋ค๋ฌ(์ปจํธ๋กค๋ฌ)๋ก ์ ๋ฌํ ์ง ๊ฒฐ์ ํ๋ ์ ๋ต ์ธํฐํ์ด์ค
- DispatcherServlet โ HandlerMapping โ Controller ํธ์ถ ๊ตฌ์กฐ
- ๋ํ์ ์ธ ๊ตฌํ์ฒด:
BeanNameUrlHandlerMapping
SimpleUrlHandlerMapping
RequestMappingHandlerMapping
2๏ธโฃ HandlerMapping ์ค์ ์ฝ๋
โ
BeanNameUrlHandlerMapping
- ์์ฒญ URL๊ณผ Bean ์ด๋ฆ์ ๋งค์นญ
- ์์ ์ฝ๋:
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
return new BeanNameUrlHandlerMapping();
}
@Bean("/custom_01")
public CustomHandler customHandler() {
return new CustomHandler();
}
โ
SimpleUrlHandlerMapping
- ๊ฐ๋ฐ์๊ฐ ์ง์ URL-Handler๋ฅผ ๋งคํํ๋ ๋ฐฉ์
- ์ฃผ๋ก ์ ์ ์์(css, js ๋ฑ)์ ๋ํ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์ ๋๋ค.
- ์์ ์ฝ๋:
@Bean
SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/custom_02", new CustomHandler());
handlerMapping.setUrlMap(urlMap);
return handlerMapping;
}
โ
RequestMappingHandlerMapping
@RequestMapping
, @GetMapping
, @PostMapping
๋ฑ์ ํตํ ๋งคํ
- ์คํ๋ง ๋ถํธ์์๋ ๊ธฐ๋ณธ ์ค์
- ์ง์ ๋ฑ๋กํ๋ ์์:
@Bean
RequestMappingHandlerMapping requestMappingHandlerMapping() throws NoSuchMethodException, SecurityException {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
RequestMappingInfo mappingInfo = RequestMappingInfo
.paths("/custom_03")
.methods(RequestMethod.GET)
.build();
Method method = CustomHandler.class.getMethod("hello", null);
handlerMapping.registerMapping(mappingInfo, new CustomHandler(), method);
return handlerMapping;
}
3๏ธโฃ HandlerMapping์ ์ปค์คํ
ํ๊ฒ ์ฌ์ฉํ๋ ์ด์
โ
๊ธฐ๋ณธ ์ ๊ณต ๋งคํ๋ง์ผ๋ก๋ ํ๊ณ๊ฐ ์์ ๋
- ์ธ๋ถ ์์คํ
์ฐ๋์ด๋, ๊ท์น ์๋ ๋ค์ํ URL ๋์์ด ํ์ํ ๋
- ๋ณต์กํ ์์ฒญ ๋ถ๊ธฐ ๋ก์ง์ด ํ์ํ ๊ฒฝ์ฐ
โ
์์ฒญ URL โ ํธ๋ค๋ฌ ๋งคํ์ ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ก ์ ์ดํ๊ณ ์ถ์ ๋
- ๋ฐํ์์ ๋งคํ์ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํด์ผ ํ ๊ฒฝ์ฐ
- ์ค์ ํ์ผ์ ์ฝ์ด ๋งคํ์ ๋์ ์ผ๋ก ์ ์ฉํ๋ ๊ฒฝ์ฐ
โ
๋ณต์กํ ๋งคํ ์ ์ฑ
์ ๊ตฌํํ๊ณ ์ถ์ ๋
- ๋จ์ ๊ฒฝ๋ก ๋งค์นญ์ด ์๋๋ผ, ์์ฒญ Header, Content-Type, IP ์ฃผ์ ๋ฑ์ ๋ฐ๋ผ ๋งคํ์ ๋ฌ๋ฆฌํด์ผ ํ ๋
โ
์คํ๋ง ๊ธฐ๋ณธ ๋งคํ ๋ฐฉ์์ ํ์ฅํ๊ฑฐ๋ ๋ณด์ํ๊ณ ์ถ์ ๋
- ๊ธฐ์กด ์ปจํธ๋กค๋ฌ ๋งคํ์ ์ ์งํ๋ฉด์ ๋ณ๋ ์ปค์คํ
์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ ํ ๋
4๏ธโฃ CustomHandler ํด๋์ค
โ
CustomHandler ์์
public class CustomHandler implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("CustomHandler's handleRequest");
return null;
}
public void hello() {
System.out.println("CustomHandler's hello!!...");
}
}
5๏ธโฃ ์๋ฒ ์คํ ๊ฒฐ๊ณผ
๐น ๋งคํ ํ์ธ ๋ก๊ทธ
- BeanNameUrlHandlerMapping:
Mapped URL path [/custom_01] onto handler '/custom_01'
- SimpleUrlHandlerMapping:
Mapped URL path [/custom_02] onto handler of type [class com.example.app.handler.CustomHandler]
- RequestMappingHandlerMapping:
Mapped "{[/custom_03],methods=[GET]}" onto public void com.example.app.handler.CustomHandler.hello()
๐น ์ถ๋ ฅ ๊ฒฐ๊ณผ
๐ ์ฐธ๊ณ ์๋ฃ
๐ง ๋๋ ์
- Spring MVC์ ํต์ฌ ์ฒ๋ฆฌ ๊ณผ์ ์ธ
HandlerMapping
์ ์ธ ๊ฐ์ง ์ฃผ์ ๋ฐฉ์์ ์ฝ๋๋ก ์ง์ ๊ตฌ์ฑํด๋ณด๋ฉด์ ๋์ ์๋ฆฌ๋ฅผ ๋ช
ํํ ์ดํดํ ์ ์์๋ค.
- ํนํ
RequestMappingHandlerMapping
์ ํตํด Reflection
์ ์ฌ์ฉํ์ฌ ๋ฉ์๋๋ฅผ ์๋์ผ๋ก ๋ฑ๋กํ ์ ์๋ค๋ ์ ์ด ๊ต์ฅํ ํฅ๋ฏธ๋ก์ ๋ค.
- ์ค์ต์ ํตํด DispatcherServlet์ด ๋ค์ํ HandlerMapping์ ํตํด ์์ฒญ์ ๋งคํํ๊ณ ์ฒ๋ฆฌํ๋ ์ ์ฒด ๊ณผ์ ์ ์ฒดํํ ์ ์์๋ค.
โ๏ธ ์์ฝ
HandlerMapping
์ ์์ฒญ URL๊ณผ Controller/Method๋ฅผ ์ฐ๊ฒฐํ๋ ํต์ฌ ์ธํฐํ์ด์ค๋ค.
- ์ฃผ์ ๊ตฌํ์ฒด:
BeanNameUrlHandlerMapping
: Bean ์ด๋ฆ๊ณผ URL ๋งค์นญ
SimpleUrlHandlerMapping
: ๊ฐ๋ฐ์๊ฐ ์ง์ ๋งคํ ์ค์
RequestMappingHandlerMapping
: @RequestMapping ๊ธฐ๋ฐ ๋งคํ
- ์ปค์คํ
HandlerMapping์ ๋ณต์กํ๊ฑฐ๋ ๋์ ์ธ ์์ฒญ ์ฒ๋ฆฌ๋ฅผ ์ํด ํ์์ ์ด๋ค.
- ์๋ฒ ๋ก๊ทธ๋ฅผ ํตํด ๋งคํ ๊ณผ์ ์ ๋ช
ํํ ํ์ธํ ์ ์๋ค.