//代码千万行,思路很重要
//借鉴他人思路然后自己写了下,有可能代码有错,因为在文本文档打的,只供交流。
public ExtDispacherServlet extends Httpservlet{
// mvc bean key=beanid ,value=对象
private CurrentHashMap<String,Object> mvcBeans=new CurrentHashMap<String,Object>;
// mvc 请求方法 key=requestUrl,value=对象
private CurrentHashMap<String,Object> mvcBeanUrl=new CurrentHashMap<String,Object>;
// mvc 请求方法 key=requestUrl,value=方法
private CurrentHashMap<String,String> mvcMethods=new CurrentHashMap<String,Object>;
/**
* 初始化自定义SpringMVC容器
*/
pubic void init(){
//扫描包,获取包下所有的类
List<Class<?>> classes = ClassUtil.getClasses("com.itmayiedu.ext.controller");
//初始化所有的类,放在mvcBeans中
findClassMvcBeans(classes);
// 3.初始化HandlerMapping方法,将url和方法对应上
handlerMapping(mvcBeans);
}
private void findClassMvcBeans(List<Class<?>> classes ){
classes.stream().foreach(class ->{
ExtController extController =class.getDeclaredAnnotation(ExtController.class);
if(extController!=null){
//初始化bean,加入容器中
String beanId = ClassUtil.toLowerCaseFirstOne(class.getSimpleName());
mvcBeans.put(beanId,)
}
})
}
private Object newInstance(Class classinfo){
if(classinfo!=null){
return classinfo.newInstance();
}else{
return null;
}
}
private void handlerMapping(ConcurrentHashMap<String, Object> mvcBeans){
//遍历所有的类,找到类中有注解的方法,将url地址放入集合中
for(Map.Entry<String, Object> entry : mvcBeans.entrySet()){
//或得类
Object mvcObject=entry.getValue();
//判断类上是否有@ExtRequestMapping注解
Class classinfo=object.getClass();
ExtRequestMapping extRequestMapping=classinfo.getDeclaredAnnotation(ExtRequestMapping.class);
String requestUrl=null;
if(extRequestMapping!=null){
requestUrl=extRequestMapping.getValue();
}
//遍历类,找到有@ExtRequestMapping注解的方法
for(Method methos:classinfo.getDeclaredMethods){
ExtRequestMapping extRequestMapping = methos.getDeclaredAnnotation(ExtRequestMapping.class);
if(extRequestMapping!=null){
requestUrl+=extRequestMapping.getValue();
}
}
mvcBeanUrl.put(requestBaseUrl, mvcObject);
mvcMethodUrl.put(requestBaseUrl, method.getName());
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
doDispatch(req, resp);
} catch (Exception e) {
// TODO: handle exception
}
}
private void doDispatch(HttpServletRequest req, HttpServletResponse resp){
//获取请求地址
String requestUrl=req.getRequestUrl();
//在集合中找到对应的地址
Object object=mvcBeanUrl.getValue(requestUrl);
if(object==null){
resp.getWriter().println("404 not found");
return;
}
//找到对应的方法名称
String method=mvcMethods.getValue(requestUrl);
if(method==null){
resp.getWriter().println("404 not found");
}
//通过反射技术执行方法并得到返回值
Clsss classinfo=object.getClass();
Object result=method.invoke(object,method);
//视图进行渲染
viewdisplay(result,req,resp);
}
public void viewdisplay(String pageName, HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// 获取后缀信息
String suffix = ".jsp";
// 页面目录地址
String prefix = "/";
req.getRequestDispatcher(prefix + pageName + suffix).forward(req, res);
}
}
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtRequestMapping {
String[] value();
}
SpringMVC底层源码思路
最新推荐文章于 2024-07-14 11:32:09 发布