SpringMVC的零配置实现

传统方式配置方式通过个web.xml 配置 org.springframework.web.servlet.DispatcherServlet,实现请求的路由和相关流程的控制。

当前基于Spring 版本5.0.2.RELEASE

web.xml中的基本配置

	
		SpringMVC
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-servlet.xml
		
		1
	
	
		SpringMVC
		/
	

阅读源代码发现底层一样实现WebApplicationInitializer并重写onStartup

取代web.xml

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import com.digimaple.core.constant.Repository;
import com.digimaple.core.constant.Sys;

/**
 * @ClassName WebOnStartup
 * @Description 容器启动装载配置
 * @author Cheng.Wei
 * @date 2017年12月9日 上午0:25:35
 * 
 */
public class WebOnStartup implements WebApplicationInitializer{

	/* (non-Javadoc)
	 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
	 */
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext webContext = createWebContext(WebMvc.class);
		Dynamic dynamic = servletContext.addServlet(Sys.SERVLET_NAME, new DispatcherServlet(webContext));
		dynamic.addMapping("/");
		dynamic.setLoadOnStartup(1);
		
	}
	/**
	 * 自定义配置类来实例化一个Web Application Context
	 * @param annotatedClasses
	 * @return
	 */
	private AnnotationConfigWebApplicationContext createWebContext(Class... annotatedClasses) {
		AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
		webContext.register(annotatedClasses);
		return webContext;
	}
}


spring-servlet.xml配置



    
    
        
    
    
    
    
    
        
    
    


取代spring-servlet.xml


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

/**
 * @ClassName WebMvc
 * @Description 表现层配置
 * @author Cheng.Wei
 * @date 2017年12月9日 上午0:40:00
 * 实现 {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurer}
 */
@Configuration  
@EnableWebMvc  
@ComponentScan(basePackages = "org.dhweicheng",includeFilters= {@ComponentScan.Filter(type=FilterType.ANNOTATION, value= {org.springframework.stereotype.Controller.class})})
public class WebMvc implements WebMvcConfigurer{
	protected static final Logger logger = LogManager.getLogger(WebMvc.class);
	 	
	@Bean
	public LocalValidatorFactoryBean validator() {
		logger.debug("LocalValidatorFactoryBean");
		return new LocalValidatorFactoryBean();
	}
	@Bean
	public BeanNameViewResolver beanNameViewResolver() {
		logger.debug("BeanNameViewResolver");
		return new BeanNameViewResolver();
	}
	@Bean
	public MappingJackson2JsonView jsonView() {
		logger.debug("MappingJackson2JsonView");
		MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
		mappingJackson2JsonView.setContentType("text/html;charset=UTF-8");
		return mappingJackson2JsonView;
	}
	/**激活静态资源处置策略*/
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
}

至此可以删除web.xml配置和springMVC配置,而这均已通过java代码实现。


你可能感兴趣的:(spring)