IBATIS/myBatis
2018.07.30 / 11:28

Spring 4.0 + Java Config - web.xml ¾ø¾Ö±â...

summerman
Ãßõ ¼ö 281

ÁøÂ¥ ¿À·¡°£¸¸¿¡ ´Ù½Ã ½ºÇÁ¸µÀ» º¸±â ½ÃÀÛÇß´Ù.


¿ª½Ã ¿¹»óÀº ÇßÁö¸¸ Âü ¸¹À̵µ ¹Ù²î¾ú´Ù.


³»°¡ ´À³¢±â¿¡ Å« º¯È­¸¦ µü 2°¡Áö°í Àû¾îº¸ÀÚ¸é,,


1. ºôµåÅøÀ» maven¿¡¼­ gradle·ÎÀÇ ÀÌÀüÀÌ´Ù...½ºÇÁ¸µ¿¡¼­´Â °ø½ÄÀûÀ¸·Î gradle¸¦ ¸ÞÀÎÀ¸·Î Áö¿øÇÏ·Á´Â ¸ð¾çÀÌ´Ù.


2. configurationÀÌ xml¿¡¼­ java config·ÎÀÇ º¯°æ.

ÀÌ°Ç ¾Æ¸¶µµ ¿äÁò °³¹ß Ãß¼¼°¡ ÃÖ´ëÇÑ ´ÜÀÏ ¾ð¾î¸¦ »ç¿ëÇÑ °³¹ßÀ» Ãß±¸ÇÏ´Â °æÇâ¿¡ Æí½ÂÇÏ´Â µí ÇÏ´Ù.

Âü°í·Î servlet-api 3.x¿¡¼­ºÎÅÍ Áö¿øµÇ´Â°É·Î ¾Ë°í ÀÖ´Ù.


¸» ÇÏ¸é ¹¹Çϳª,,ÇÔ ÇغÁ¾ßÁö..

ÀÏ´Ü ÇÁ·ÎÁ§Æ® Æ®¸®´Â ´ÙÀ½°ú °°´Ù.



¿ë°¨ÇÏ°Ô web.xml ÆÄÀÏÀ» Á¦°Å(¾Æ´Ô Àúó·³ È®ÀåÀÚ¸¦ º¯°æÇصμŵµ µË´Ï´Ù.)


½ºÇÁ¸µ IDE Â÷¿ø¿¡¼­ ÇØ Áà¾ß ÇÒ °ÍÀº ½ºÇÁ¸µ¿¡¼­ java configÆÄÀÏ¿¡ ´ëÇÑ, Áï @Configuration annotationÀ» ÀÚµ¿À¸·Î ãÀ» ¼ö ÀÖµµ·Ï ÇØÁà¾ß ÇÑ´Ù. ¾È±×·¯¸é ÀÏÀÏÈ÷ Ãß°¡ÇØÁà¾ß ÇϹǷΠ±ÍÂú´Ù.



±× ´ÙÀ½À¸·Î ÇØÁà¾ß ÇÒ °ÍÀº Context ·Îµå½Ã¿¡ ÃʱâÈ­½ÃÄÑÁÙ ¼³Á¤ ÆÄÀÏÀ» ¸¸µé¾îÁÖ´Â °ÍÀÌ´Ù.



package com.figo.web.initializer;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebInitializer implements WebApplicationInitializer {
	private static final String CONFIG_LOCATION = "com.figo.web.config";
	private static final String MAPPING_URL = "/";
	
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		WebApplicationContext context = getContext();
		servletContext.addListener(new ContextLoaderListener(context));
		ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
		dispatcher.setLoadOnStartup(1);
		dispatcher.addMapping(MAPPING_URL);
	}
	private AnnotationConfigWebApplicationContext getContext() {
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.setConfigLocation(CONFIG_LOCATION);
		return context;
	}
}


Âü°í·Î mapping_urlÀ» /* ·Î Çß´õ´Ï, view ÆÄÀÏÀÎ jsp ÆÄÀÏÀ» ÆÄ½Ì ¸øÇÏ´Â ¹®Á¦°¡ ÀÖ¾ú´Ù.

ÇØ°á¿¡ µµ¿òÀ» ÁֽŠstackoverflow¿¡ °í¸¶¿òÀ» ´Ù½Ã Çѹø ~~ ¤¾¤¾¤¾

½ßÀ¯


´ÙÀ½ ´Ü°è·Î servlet context¸¦ ´ëüÇÒ config ÆÄÀÏÀ» ¸¸µç´Ù.


package com.figo.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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;

@EnableWebMvc // ¿¡ ÇØ´ç.
@ComponentScan(basePackages = {"com.figo.web"})  // ¿¡ ÇØ´çµÊ.
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    // ¿¡ ÇØ´çµÊ.
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
    }

    // ¿¡ ÇØ´çµÊ.
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }    
    
    // ¹¹ À̺κÐÀº ´Ù µé ¾Æ½Çµí..~~
    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/web").setViewName("home");
        registry.addViewController("/").setViewName("home");
    }

}


³¡ÀÌ´Ù..~~

xml ÆÄÀÏÀ» µû·Î ¾ÈºÁµµ µÇ´Ï ±ò²ûÇÏ´Ù´Â »ç¶÷µµ ÀÖÀ¸³ª,

¼ÖÂïÈ÷ ¸»Çؼ­ ³­ Àß ¸ð¸£°Ú´Ù.

´ÙÀ½´Ü°è¸¦ ¾Æ¸¶µµ security ºÎºÐÀ» Ãß°¡ÇغÁ¾ß ÇÒ µí ½Í´Ù.