爲何要造這個輪子?如今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