SpringMVC的零配置实现

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

当前基于Spring 版本5.0.2.RELEASE

web.xml中的基本配置

	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


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

取代web.xml


/**
 * @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配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">
    <!-- SpringMVC配置信息  -->
    <context:component-scan base-package="org.dhweicheng">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    <mvc:annotation-driven/>
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
    <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
        <property name="contentType" value="text/html;charset=UTF-8"/>
    </bean>
    <mvc:default-servlet-handler/>
</beans>

取代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代码实现。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值