为何要造这个轮子?如今Java领域的mvc框架层出不穷,springmvc,struts2,jfinal;容器方面有tomcat,jetty,undertow。为何要造这个不成熟的东西出来?我想起我在大二刚接触java web是学的struts2,一大堆xml配置让我看到吐,感受这掩盖了网络编程的原本面目,为何不从底层的socket写起,解析http协议,封装请求和响应,我以为这样更能理解本质。因而我造了第一个轮子做为这个想法的验证MineServer, 这个轮子如今放在github上,但这只是一个玩具项目,我想造的是一个完整的轮子,因而我开了这个坑Boomvc。java
我想要一个相似spring boot这种开发体验的web框架,能够用一行代码开启一个http server,没有xml配置,使用注解和简单的properties配置文件,最简的依赖,能够不用遵照servlet规范的web mvc框架。框架要包括3个部分:底层的http server和上层的mvc框架,以及ioc容器。http server能够本身从socket实现,也可使用netty这样的网络框架实现。git
目前框架已经基本实现完成,还有不少bug,可是完成了本身的想法,我感到很开心。目前框架实现了一下的功能。github
能够像spring boot 那样一行代码启动 :)web
public static void main(String[] args) { Boom.me().start(Main.class, args); }
写一个controllerspring
@RestPath public class TestController { @GetRoute("/") public String hello(){ return "hello world"; } }
同时还有过滤器和拦截器编程
public interface Filter { void init(FilterConfig config); void doFilter(HttpRequest request, HttpResponse response, FilterChain chain) throws Exception; void destroy(); } public interface Interceptor { boolean preHandle(HttpRequest request, HttpResponse response); void postHandle(HttpRequest request, HttpResponse response, ModelAndView modelAndView); void afterCompletion(HttpRequest request, HttpResponse response, Exception e); }
而后像spring boot那样注册,写一个配置类继承WebMvcConfigurerAdapterapi
@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(WebMvcRegistry registry) { Interceptor interceptor = new MyInterceptor(); registry.addInterceptor(interceptor) .order(1) .patternUrl("/*"); } @Override public void addFilters(WebMvcRegistry registry) { Filter filter = new MyFilter(); registry.addFilter(filter) .addFilterInitParameter("hello", "world") .addFilterInitParameter("ni", "hao") .addFilterPathPattern("/*") .order(1); } }
作为一个开源的新手,我写了这个项目,有不少不足和bug,但愿有大牛能够多多指教。若是你有兴趣,欢迎star,fork,提issues,一块儿共同窗习讨论Boomvc😊tomcat