WebMvcConfig.java 파일은 application-context.xml 파일을 대체하는 파일입니다.

WebMvcConfig을 설명하기 앞서 WebInitializer.java에서 호출한 설정파일 AppConfig 파일에 대해 간단히 설명하도록 하겠습니다.

 

AppConfig.java

package com.intercast.web.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.intercast.web.config.security.SecurityConfig;

@Configuration
@Import({WebMvcConfig.class, CommonConfig.class, SecurityConfig.class})
public class AppConfig {

}

정말 간단합니다.

붉은색으로 처리한 부분을 보시면 설정파일이라고 선언을 하고

사용하고자 하는 설정 파일들은 @Import 해 주면 끝입니다. 추가적으로 더 생성이 되면 @Import에 더 추가 해 주시면 됩니다. 

간단하죠...ㅋㅋ

 

그럼 다음으로 WebMvcConfig.java 입니다.

package com.intercast.web.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;

import com.intercast.util.pagination.CommonPaginationRenderer;

import egovframework.rte.ptl.mvc.tags.ui.pagination.DefaultPaginationManager;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationRenderer;

 

@Configuration
/* <mvc:annotation-driven /> */
@EnableWebMvc


/*
<context:component-scan base-package="com.intercast">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
 </context:component-scan>
*/
@ComponentScan(
  basePackages="com.intercast",
    includeFilters={
    @ComponentScan.Filter(Controller.class)
  },
  excludeFilters={
    @ComponentScan.Filter(Service.class),
    @ComponentScan.Filter(Repository.class)
  }
 )
public class WebMvcConfig extends WebMvcConfigurerAdapter {

 //<resources location="/resources/" mapping="/resources/**">에 해당됨.
 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
 }

 

 //<mvc:default-servlet-handler>에 해당됨.
 @Override
 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  configurer.enable();
 }

 

 /**
  * <pre>
  * Return Type을 JSON으로 사용하고 싶을 경우 설정을 해줘야 한다.<br />
  * 없을 경우
  * "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
  * 오류가 발생한다. <br />
  *
  * XML 설정은 아래와 같다.
  * <mvc:annotation-driven  content-negotiation-manager="contentNegotiationManager" />
  * <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  *      <property name="favorPathExtension" value="false" />
  *      <property name="favorParameter" value="true" />
  *      <property name="mediaTypes" >
  *           <value>
  *                json=application/json
  *                xml=application/xml
  *           </value>
  *      </property>
  * </bean>
  * </pre>
  */
 @Override
 public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
  configurer.favorPathExtension(false)
     .favorParameter(true)
     .defaultContentType(MediaType.APPLICATION_JSON)
     .mediaType("xml", MediaType.APPLICATION_ATOM_XML)
     .mediaType("json", MediaType.APPLICATION_JSON);
 }

 

 /* <mvc:view-controller path="/accessDenied" view-name="error/accessDenied"/> */
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
  registry.addViewController("/accessDenied.htm").setViewName("error/accessDenied");
 }

 

 /**
  * Tiles 설정파일
  *
  * <bean id="titlesConfigurer"
  *  class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
  *  <property name="definitions">
  *   <list>
  *    <value>/WEB-INF/config/tiles-defs.xml</value>
  *   </list>
  *  </property>
  * </bean>
  */
 @Bean
 public TilesConfigurer tilesConfigurer() {
  TilesConfigurer configure = new TilesConfigurer();
  configure.setDefinitions("/WEB-INF/config/tiles-defs.xml");;
  return configure;
 }

 

 /**
  * <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  *  <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"></property>
  *  <property name="order" value="1"></property>
  * </bean>
  */
 @Bean
 public UrlBasedViewResolver urlBasedViewResolver() {
  UrlBasedViewResolver resolver = new UrlBasedViewResolver();
  resolver.setViewClass(TilesView.class);
  resolver.setOrder(1);
  return resolver;
 }

 

 /**
  * <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  *  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
  *  <property name="prefix" value="/WEB-INF/jsp/"></property>
  *  <property name="suffix" value=".jsp"></property>
  *  <property name="order" value="2"></property>
  * </bean>
  */
 @Bean
 public InternalResourceViewResolver internalResourceViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setViewClass(JstlView.class);
  resolver.setPrefix("/WEB-INF/views/");
  resolver.setSuffix(".jsp");
  resolver.setOrder(2);
  return resolver;
 }

 

 /**
  * MultipartResolver 설정
  *
  * <bean id="multipartResolver"
  *  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  *  <property name="maxUploadSize" value="200000000" />
  *  <property name="maxInMemorySize" value="100000000" />
  * </bean>
  */
 @Bean
 public MultipartResolver multipartResolver() {
  CommonsMultipartResolver resolver = new CommonsMultipartResolver();
  resolver.setMaxInMemorySize(100000000);
  resolver.setMaxUploadSize(200000000);
  return resolver;
 }

 

 /**
  * 전자정부프레임워크의 Paging 처리 Lib 사용하여
  * Paging 설정
  *
  * <bean id="commonPaginationRenderer" class="com.intercast.util.pagination.CommonPaginationRenderer" />
     *
  * <bean id="paginationManager" class="egovframework.rte.ptl.mvc.tags.ui.pagination.DefaultPaginationManager">
  *  <property name="rendererType">
  *   <map>
  *    <entry key="common" value-ref="commonPaginationRenderer"></entry>
  *   </map>
  *  </property>
  * </bean>
  */
 @Bean
 public DefaultPaginationManager paginationManager() {
  DefaultPaginationManager manager = new DefaultPaginationManager();

  Map<String, PaginationRenderer> renderer = new HashMap<String, PaginationRenderer>();
  renderer.put("common", new CommonPaginationRenderer());
  manager.setRendererType(renderer);

  return manager;
 }


 /**
  * Message properties 설정
  *
  * <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  *  <property name="basenames">
  *   <list>
  *    <value>properties.messages</value>
  *   </list>
  *  </property>
  * </bean>
  */
 @Bean public MessageSource messageSource(){
    ReloadableResourceBundleMessageSource messageSource=new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames(
      "classpath:properties/messages"
   );
    return messageSource;
  }

}

저는 ebMvcConfigurerAdapter 를 상속 받아 사용하였습니다.

또는 WebMvcConfigurer 인터페이스를 구현하여 사용하여도 됩니다.

 

각 메소드 또는 Annotation 상단에 주석으로 해 놓은 부분이 XML에 설정하는 내용들입니다.

저도 설정을 해 보면서 XML 과 별반 다를게 없다는 생각을 많이 하였습다..ㅡㅡ;;;

XML에서 에서 Bean에 설정되어 있는 내용을 하나의 메소드로 만들고 해당 메소드에서 XML Bean class 속성에 있는 Class를 선언하고 Return하는 그런 방식입니다..ㅋ

보시는 바와 같이 @Override를 하지 않고 생성한 Method들은 모두 @Bean을 사용하여 Bean에 등록을 합니다.

이렇게 간단히 설명을 하고 넘어가도록 하겠습니다....^^

 

+ Recent posts