spring 3.0 mvc config on source code / source code config / spring java based configuration / 자바로 config / 자바로 설정 / configuration on java / spring code based configuration
IntelliJ 에서 Spring 3.0 에서 도입된 Java based configuration 을 사용해서 간단한 project 를 만들어 보자.
Sprinb MVC project 생성
아래 설명을 따라서 Spring MVC project 를 생성하자.
그러면 기본적으로
- mvc-dispatcher-servlet.xml
Java based configuration 으로 전환
이제 이녀석을 mvc-dispatcher-servlet.xml 대신에 MvcConfiguration.java 를 사용하도록 수정 해 보자. 전환하는 절차는 아래와 같다.
- MvcConfiguration 추가
- mvc-dispatcher-servlet.xml 삭제
- web.xml 수정
MvcConfiguration 추가
먼저 MvcConfiguration 을 만들자.// MvcConfiguration.java package com.namh.newui.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration /** * <context:component-scan base-package="com.springapp.mvc"/> */ @ComponentScan("com.namh.newui") @EnableWebMvc public class MvcConfiguration extends WebMvcConfigurerAdapter{ /** * * <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> * <property name="prefix" value="/WEB-INF/pages/"/> * <property name="suffix" value=".jsp"/> * </bean> * */ @Bean public ViewResolver getViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } /** * <mvc:resources mapping="/resources/**" location="/resources/" /> */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } }
mvc-dispatcher-servlet.xml 삭제
그리고 이제 사용하지 않는 mvc-dispatcher-servlet 을 삭제하자.그러면 아래 그림처럼 파일들이 남을 것이다.
web.xml 수정
이제 web.xml 을 수정하자. 이 곳에서 mvc-dispatcher-servlet.xml 을 사용하도록 설정이 되는데, 여기를 수정하자.(참고 : How to configure Spring MVC with pure Java-based configuration? - Stack Overflow)... <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
위의 설정을 아래처럼 수정하자.
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd"> ... <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <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.namh.config.MvcConfiguration</param-value> </init-param> </servlet>
Project Structure 수정
그리고 intelliJ 설정을 조금 수정 해 주자.- File > Project Structure > Modules > Spring > MVC mvc-dispatcher servlet context
에서 mvc-dispatcher-servlet.xml 대신에 MvcConfiguration.java 를 사용하도록 수정하자.
댓글 없음:
댓글 쓰기