예제 hellospring2 -> hellospring3
기본 메이븐 프로젝트 생성 및 라이브러리, pom 설정 완료 후
src/main/java에
com.douzone.hellospring.initializer 패키지 추가
안에 HelloSpringWebApplicationInitializer 클래스 생성
HelloSpringWebApplicationInitializer 클래스는 web.xml의 역할을 대신하는 기능들을 가진다.
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>hellospring</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- Front Controller --> <servlet> <servlet-name>spring</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>com.douzone.hellospring.config.WebConfig</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- 톰캣이 뜰때 이 컨테이너가 만들어짐 --> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> | cs |
HelloSpringWebApplicationInitializer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.douzone.hellospring.initializer; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import com.douzone.hellospring.config.WebConfig; public class HelloSpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected String[] getServletMappings() { return new String[] {"/"}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] {WebConfig.class}; } @Override protected Class<?>[] getRootConfigClasses() { return null; } } | cs |
1. servlet-mapping 옮기기
web.xml에서
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet-mapping 부분을
HelloSpringWebApplicationInitializer에서
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
로 처리해줌
2. servlet 옮기기
web.xml에서
<!-- Front Controller -->
<servlet>
<servlet-name>spring</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>com.douzone.hellospring.config.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!-- 톰캣이 뜰때 이 컨테이너가 만들어짐 -->
</servlet>
servlet 부분을
HelloSpringWebApplicationInitializer에서
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class};
}
로 처리해줌
3. welcome-file-list부분은
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
웹 요청 시 지정되어 있는 리소스 call
즉, http://localhost:8080 요청 시 index.jsp의 content를 보여주는데
이미 main에서 잡아주기때문에
이 과정을 거치면 web.xml이 없어도 돌아간다.
728x90
'풀스택 > Spring -Junit' 카테고리의 다른 글
mysite4, bean-wiring (0) | 2019.03.21 |
---|---|
스프링 xml과 javaconfig 같이 쓰기 - guestbook4 (0) | 2019.03.20 |
spring - bean wiring - javaConfig (0) | 2019.03.20 |
Junit - XML로 빈 와이어링하기 (0) | 2019.03.19 |
댓글