从零写一个具有IOC-AOP-MVC功能的框架学习笔记

文章资讯 2020-07-16 22:12:43

从零写一个具有IOC-AOP-MVC功能的框架学习笔记

1.本章需要完成的内容
完成请求处理器接口RequestProcessor的编写
完成ResultRender渲染器接口的编写
完成DefultResultRender默认渲染器类的编写
完成InternalErrorResultRender内部异常渲染器类的编写
完成JsonResultRenderJson渲染器类的编写
完成ResourceNotFoundResultRender资源未找到时使用的渲染器类编写
完成ViewResultRender页面渲染器类的编写
完成DisatcherServlethtt请求拦截器的编写
完成RequestProcessorChain责任链类的编写
2.第一部分内容
该部分完成DisatcherServlet与RequestProcessorChain类的编写
2.1DisatcherServlet类
2.1.1需要完成的代码如下:
ackagecom.wuyiccc.helloframework.mvc;imortcom.wuyiccc.helloframework.ao.AsectWeaver;
imortcom.wuyiccc.helloframework.core.BeanContainer;
imortcom.wuyiccc.helloframework.injection.DeendencyInjector;
imortcom.wuyiccc.helloframework.mvc.rocessor.RequestProcessor;
imortcom.wuyiccc.helloframework.mvc.rocessor.iml.ContrlerRequestProcessor;
imortcom.wuyiccc.helloframework.mvc.rocessor.iml.JsRequestProcessor;
imortcom.wuyiccc.helloframework.mvc.rocessor.iml.PreRequestProcessor;
imortcom.wuyiccc.helloframework.mvc.rocessor.iml.StaticResourceRequestProcessor;
imortlombok.extern.slf4j.Slf4j;imortjavax.servlet.ServletExcetion;
imortjavax.servlet.annotation.WebServlet;
imortjavax.servlet.htt.HttServlet;
imortjavax.servlet.htt.HttServletRequest;
imortjavax.servlet.htt.HttServletResonse;
imortjava.io.IOExcetion;
imortjava.io.InutStream;
imortjava.util.ArrayList;
imortjava.util.List;
imortjava.util.Proerties;**
*@authorwuyiccc
*@date202071421:42
*岂曰无衣,与子同袍~
*
@WebServlet("*")
@Slf4j
ubcclassDisatcherServletextendsHttServlet{rivatestaticfinalStringHELLOFRAMEWORK_CONFIG_FILE="confighelloframework-config.roerties";
rivateList<RequestProcessor>PROCESSOR=newArrayList<>();@Override
ubcvoidinit()throwsServletExcetion{1.初始化容器
BeanContainerbeanContainer=BeanContainer.getInstance();beanContainer.loadBeans(getHelloFrameworkScanPackages());
newAsectWeaver().doAo();
newDeendencyInjector().doIoc();
2.初始化请求处理器责任链
PROCESSOR.add(newPreRequestProcessor());
PROCESSOR.add(newStaticResourceRequestProcessor(getServletContext()));
PROCESSOR.add(newJsRequestProcessor(getServletContext()));
PROCESSOR.add(newContrlerRequestProcessor());
}@Override
rotectedvoidservice(HttServletRequestreq,HttServletResonseres)throwsServletExcetion,IOExcetion{
1.创建责任链对象实例
RequestProcessorChainrequestProcessorChain=newRequestProcessorChain(PROCESSOR.iterator(),req,res);
2.通过责任链模式来依次调用请求处理器对请求进行处理
requestProcessorChain.doRequestProcessorChain();
3.对处理结果进行渲染
requestProcessorChain.doRender();
}rivateStringgetHelloFrameworkScanPackages(){
Proertiesroerties=newProerties();
InutStreamin=Thread.currentThread().getContextClassLoader().getResourceAsStream(HELLOFRAMEWORK_CONFIG_FILE);
try{
roerties.load(in);
}catch(IOExcetione){
log.warn("Theconfighelloframework.roertiescannotload");
e.rintStackTrace();
}StringscanPackages=roerties.getProerty("helloframework.scan.ackages");
log.info("thisisscanPackages:{}",scanPackages);
returnscanPackages;
}
}2.1.2DIsatcherServlet类相关代码讲解:
2.2RequestProcessorChain类
2.2.1需要完成的代码如下:
ackageorg.myframework.mvc;**
*@authorwuyiccc
*@date202061618:48
*岂曰无衣,与子同袍~
*imortlombok.Data;
imortlombok.extern.slf4j.Slf4j;
imortorg.myframework.mvc.rocessor.RequestProcessor;
imortorg.myframework.mvc.render.DefaultResultRender;
imortorg.myframework.mvc.render.InternalErrorResultRender;
imortorg.myframework.mvc.render.ResultRender;imortjavax.servlet.htt.HttServletRequest;
imortjavax.servlet.htt.HttServletResonse;
imortjava.util.Iterator;**
*1.以责任链的模式执行注册的请求处理器
*2.委派给特定的Render实例对处理后的结果进行渲染
*
@Data
@Slf4j
ubcclassRequestProcessorChain{请求处理器迭代器
rivateIterator<RequestProcessor>requestProcessorIterator;rivateHttServletRequestrequest;rivateHttServletResonseresonse;htt请求方法
rivateStringrequestMethod;htt请求路径
rivateStringrequestPath;htt响应状态码
rivateintresonseCode;请求结果渲染器
rivateResultRenderresultRender;
ubcRequestProcessorChain(Iterator<RequestProcessor>requestProcessorIterator,HttServletRequestrequest,HttServletResonseresonse){
this.requestProcessorIterator=requestProcessorIterator;
this.request=request;
this.resonse=resonse;
this.requestMethod=request.getMethod();
this.requestPath=request.getPathInfo();
this.resonseCode=HttServletResonse.SC_OK;
}**
*以责任链的模式执行请求链
*
ubcvoiddoRequestProcessorChain(){
1.通过迭代器遍历注册的请求处理器实现类列表
try{
while(requestProcessorIterator.hasNext()){
2.知道某个请求处理器执行后返回为false为止
if(!requestProcessorIterator.next().rocess(this)){
eak;
}
}
}catch(Excetione){
3.期间如果出现异常,则交给内部异常渲染器处理
this.resultRender=newInternalErrorResultRender(e.getMessage());
log.error("doRequestProcessorChainerror:",e);
}}
**
*执行处理器
*
ubcvoiddoRender(){
1.如果请求处理器实现类均未选择合适的渲染器,则使用默认的
if(this.resultRender==null){
this.resultRender=newDefaultResultRender();
}
2.调用渲染器的render方法对结果进行渲染
try{
this.resultRender.render(this);
}catch(Excetione){
log.error("doRendererror:",e);
thrownewRuntimeExcetion(e);
}}
}2.2.2RequestProcessorChain相关代码讲解