spring实战-搭建SpringWeb应用程序

一个案例【接口文档及自动化测试工具IDAT】


在工作中遇到一个棘手的问题,就是接口的管理。当一个系统发展到足够大时,系统会被分解为很多个系统,而服务层会为不同的系统提供服务接口。当公司发展足够大时,这些系统会被分不到不同的部门,而且接口会越来越多,接口的维护也会越来越麻烦,很多时候都是靠接口文档拷贝来拷贝去,最后不同的部门拿到的接口文档不是同一个版本的,而有些人使用了别部门的接口后,开发人员走了,再新进开发人员时,完全不知道接口从哪来,接口更新是也不知道通知谁来同步更新。这时候服务层的管理,服务的注册与发现,服务接口文档的维护就至关重要。

本人打算从0开始搭建一个接口文档及接口自动化测试工具,用以在Spring知识的体系构建中练练手

系统名称:IDAT(interface document and test)

系统分层:web层,service层

Web层:标准MVC模型设计,主要面向客户端,设计用户操作逻辑编写,不直接对接数据库

Service层:基于领域对象模型设计,负责Web层的服务对接

系统使用组件:Spring,SpringMVC,MyBatis

后续可能使用Spring Cloud


第一篇:搭建SpringWeb应用程序

Dispatcher

package com.halfworlders.idat.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 只需要扩展AbstractAnnotationConfigDispatcherServletInitializer类
 * 就可以自动的配置DispatcherServlet和Spring应用上下文
 * 
 * @createTime 2017-08-13
 * @version 0.0.1
 * @author lllhappy
 *
 */
public class IdatWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	/**
	 * 该方法返回带有@Configuration注解的类,将会用来定义ContextLoaderListener应用上下文中的Bean
	 */
	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] { RootConfig.class };
	}

	/**
	 * 该方法返回带有@Configuration注解的类,将会用来定义DispatcherServlet应用上下文中的Bean
	 */
	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { WebConfig.class };
	}

	/**
	 * 将一个或多个路径映射到DispatcherServlet上
	 */
	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

}

WebConfig

package com.halfworlders.idat.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.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * 定义DispatcherServlet应用上下文中的Bean
 * 
 * @createTime 2017-08-13
 * @version 0.0.1
 * @author lllhappy
 *
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.halfworlders.idat.controller")
public class WebConfig extends WebMvcConfigurerAdapter{

	/**
	 * 配置jsp视图解析器
	 * @return
	 */
	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		resolver.setExposeContextBeansAsAttributes(true);
		return resolver;
	}

	/**
	 * 配置静态资源处理
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		// 将静态资源请求转发到servlet容器中默认的servlet上,而不是使用DefaultServlet来处理
		configurer.enable();
	}
}
RootConfig

package com.halfworlders.idat.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * 定义ContextLoaderListener应用上下文中的Bean
 * 
 * @createTime 2017-08-13
 * @version 0.0.1
 * @author lllhappy
 *
 */
@Configuration
@ComponentScan(basePackages="com.halfworlders.idat",
excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

}
HomeController

package com.halfworlders.idat.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Controller注解申明一个控制器
 * 
 * @createTime 2017-08-13
 * @version 0.0.1
 * @author lllhappy
 *
 */
@Controller
public class HomeController {

	@RequestMapping(value="/home", method=RequestMethod.GET)
	public String home(){
		// 经过视图解析器,会被解释为WEB-INF/views/home.jsp
		return "home";
	}
}
home.jsp

<html>
<body>
<h2>welcome to IDAT</h2>
</body>
</html>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1" metadata-complete="true">
	
</web-app>






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值