๐ก ์ด๋ฒ ํฌ์คํ ์์๋ ์คํ๋ง ๋ถํธ์์ resourceHandler์ ๋ํด ๊ฒช์๋ ์ด์์ ๋ํด์ ์ ๋ฆฌํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ํฉ์ ์ด๋ ์ต๋๋ค.
์คํ๋ง ๋ถํธ + ํ์๋ฆฌํ๋ก ๊ฐ๋ฐ์ ์ ํ๊ณ ์๋ค๊ฐ, ํฐ์บฃ AJP ์ค์ ์ ํด์ผ ํด์ @Configuration๊ณผ @EnableWebMvc๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ์ ํ ํ์ ๊ฐ์๊ธฐ ์ ์ Web Resource๋ค์ด ๋ชจ๋ 404 Not Found ์๋ฌ๊ฐ ๋ฐ์ํ๊ธฐ ์์ํ์ต๋๋ค.
์๋๋ ๋ฌธ์ ์ ์ฝ๋์ ๋๋ค.
@Configuration
@EnableWebMvc
@RequiredArgsConstructor
public class SpringServletConfig implements WebMvcConfigurer {
private final TomcatAjpProperties tomcatAjpProperties;
@Bean
public ServletWebServerFactory servletContainer() throws UnknownHostException {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
};
if (tomcatAjpProperties.enabled()) {
Connector ajpConnector = new Connector(tomcatAjpProperties.protocol());
ajpConnector.setPort(tomcatAjpProperties.port());
ajpConnector.setSecure(tomcatAjpProperties.secure());
ajpConnector.setAllowTrace(tomcatAjpProperties.allowTrace());
ajpConnector.setScheme(tomcatAjpProperties.scheme());
((AbstractAjpProtocol<?>) ajpConnector.getProtocolHandler()).setSecretRequired(false);
((AbstractAjpProtocol<?>) ajpConnector.getProtocolHandler()).setAddress(InetAddress.getByName("0.0.0.0"));
tomcat.addAdditionalTomcatConnectors(ajpConnector);
}
return tomcat;
}
}
์ด์ ๊น์ง ํ์ตํ๋ ๋ด์ฉ์ผ๋ก๋ ์ ์ ๋ฆฌ์์ค์ ๋ํ ์ค์ ์ ์คํ๋ง ๋ถํธ๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ค์ ํด ์ฃผ๋ฉฐ, WebProperties์ ์ ์ ๋ฆฌ์์ค์ ๋ํ ๊ฒฝ๋ก๊ฐ ์ค์ ๋์ด ์๋ค๊ณ ์๊ณ ์์์ต๋๋ค.

ํ์ง๋ง ์ด๋ฒ ์ด์์ ๋ํ ๋ฒ์ธ์ด์ ์ค๋ ์ ๋ฆฌํ ๋ด์ฉ์ธ @EnableWebMvc ๊ฐ ์ฌ์ฉ๋๋ฉด ์ ๊ฐ ์๊ณ ์๋ ๋ด์ฉ์ด ํ๋ ค์ง๊ฒ ๋ฉ๋๋ค.
๋จผ์ MVC ์ค์ ์ ์๋์ ๊ฐ์ด 2๊ฐ์ง ๋ฐฉ์์ผ๋ก ์ค์ ํ ์ ์์ต๋๋ค.
1. @Configuration + WebMvcConfigurer
2. @Configuration + @EnableWebMvc + WebMvcConfigurer
์ด๋ฒ ์ด์์ ๋ํด ์ดํดํ๋ ค๋ฉด ์ 2๊ฐ์ง ๋ฐฉ์์ ์ฐจ์ด๋ฅผ ์์์ผ ํ๋ฉฐ, ์ด์ 2๊ฐ์ง ๋ฐฉ์์ ์ฐจ์ด๋ฅผ ์ ๋ฆฌํด ๋ณด๊ฒ ์ต๋๋ค.
์คํ๋ง ๊ณต์ ๋ฌธ์์ ๊ฐ๋ณด๋ฉด ์๋์ ๊ฐ์ด ์ค๋ช ํ๊ณ ์์ต๋๋ค.


์คํ๋ง ๋ถํธ์์ ์ ๊ณตํ๋ ์ฌ์ฉ์ ์ง์ ์ค์ ์ ์ ์งํ๋ฉด์ ์ธํฐ์ ํฐ, ํฌ๋งคํฐ, ๋ทฐ ์ปจํธ๋กค๋ฌ ๋ฑ์ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด @EnableWebMvc ์ ๋ ธํ ์ด์ ์์ด @Configuration + WebMvcConfigurer๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๊ณ ํฉ๋๋ค.

๋ฐ๋๋ก ์คํ๋ง ๋ถํธ์์ ์ ๊ณตํ๋ ์ฌ์ฉ์ ์ง์ ์์ด ๋ชจ๋ ์ค์ ์ ์ ์ดํ๋ ค๋ฉด @EnableWebMvc ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ๋๋ค๊ณ ํฉ๋๋ค.
์ด์ ๊ด๋ จํด์ ๋ฐฑ๊ธฐ์ ๋์ ์ธํ๋ฐ Q&A์์๋ ๋์ผํ๊ฒ ์ค๋ช ํ๊ณ ์์ต๋๋ค.

์ค์ ์ฝ๋๋ฅผ ๋ณด๋ฉด @EnableWebMvc ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด WebMvcAutoConfiguration์ด ๋ฑ๋ก๋์ง ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.


@EnableWebMvc ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ง ์์ผ๋ฉด WebMvcAutoConfiguration์ด ๋์ํ์ฌ ๊ธฐ๋ณธ ์ ์ ๋ฆฌ์์ค์ ๋ํ resourceHandler๊ฐ ๋์ํ๊ฒ ๋ฉ๋๋ค.


๊ทธ๋์ ์ ๊ฐ ์ฒ์์ ์๊ณ ์๋ ๋ด์ฉ์ @EnableWebMvc ์ ๋ ธํ ์ด์ ์ด ์ ์ฉ๋์ง ์์ ์คํ๋ง ๋ถํธ๊ฐ ์๋์ผ๋ก ์ค์ ํด ์ฃผ์์ ๋ ์ฌ๋ฐ๋ฅธ ๋ด์ฉ์ด ๋๊ณ , @EnableWebMvc๊ฐ ์ฌ์ฉ๋๋ฉด ์คํ๋ง ๋ถํธ ์๋ ์ค์ ์ด ์ ์ฉ๋์ง ์๊ธฐ ๋๋ฌธ์ ํ๋ฆฐ ๋ด์ฉ์ด ๋๋ ๊ฒ์ ๋๋ค.
์ค๋ ํฌ์คํ ์ด์๋ resourceHandler ๋ถ๋ถ์ด์์ง๋ง ์ด ์ธ์๋ View Resolver ๋ฑ ์คํ๋ง ๋ถํธ ์๋ ์ค์ ์ ์ํด ๋ฑ๋ก๋๋ Bean๋ค์ด ์์ผ๋ ์ด ๋ถ๋ถ์ WebMvcConfigurer์ด ์ด๋ค Bean๋ค์ ์๋์ผ๋ก ๋ฑ๋กํ๋์ง ํ์ธํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
์ ๋ฆฌํ๋ฉด
์คํ๋ง ๋ถํธ ์๋ ์ค์ ์ ์ ์งํ๋ฉด์ ์ถ๊ฐ๋ก ์ค์ ํ๊ณ ์ถ์ ๊ฒฝ์ฐ์๋ @Configuration + WebMvcConfigurer
์คํ๋ง ๋ถํธ ์๋ ์ค์ ๋์ ์ง์ ๋ชจ๋ ๊ฒ์ ์ค์ ํ๊ณ ์ถ์ ๊ฒฝ์ฐ์๋ @Configuration + @EnableWebMvc + WebMvcConfigurer
๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
์ฐธ๊ณ ์๋ฃ
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.auto-configuration
- https://www.inflearn.com/questions/33417/enablewebmvc-%EC%97%86%EC%9D%B4%EB%A1%9C-%EC%97%85%EB%8D%B0%EC%9D%B4%ED%8A%B8
- https://gocandoit.tistory.com/m/30
- https://w97ww.tistory.com/82
- https://mangkyu.tistory.com/176
'Spring > Spring Boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
@ControllerAdvice & @RestControllerAdvice (0) | 2023.04.23 |
---|---|
Actuator (์ก์ธ์์ดํฐ) (0) | 2023.04.02 |
์คํ๋ง ๋ถํธ ์ฑ์ ์ด๊ธฐํ ์ฝ๋๋ฅผ ๋ฃ๋ ๋ฐฉ๋ฒ 3๊ฐ์ง (0) | 2023.03.19 |
๋๊ธ