• springboot - 手写spring mvc


    spring是一个非常流行的技术框架,其中spring mvc组件在其中非常重要的地位,主要面要客户端提供服务,我们今天来手写一个简化版的mvc,且包括ioc部分,主要利用servlet机制来实现,类的关系如下:
    在这里插入图片描述

    **

    1.准备注解类,类于spring的@Autowired、@Service、@Controller、@RequestMapping、@RequestParam

    **

    @Target({ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CSAutowired {
        String value() default "";
    }
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CSController {
        String value() default "";
    }
    
    @Target({ElementType.TYPE,ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CSRequestMapping {
        String value() default "";
    }
    
    @Target({ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CSRequestParam {
        String value() default "";
    }
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface CSService {
        String value() default "";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    **

    2.准备service interface

    **

    public interface IDemoService {
        public String get(String name);
    }
    
    • 1
    • 2
    • 3

    3.准备service实现类,利用@CSService

    @CSService
    public class DemoService implements IDemoService {
        @Override
        public String get(String name) {
            return "My name is "+name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.准备对外服务的类,主要利用@CSController注解

    @CSController
    @CSRequestMapping("/demo")
    public class DemoAction {
        @CSAutowired
        private IDemoService demoService;
    
        @CSRequestMapping("/query")
        public void query(HttpServletRequest req, HttpServletResponse resp, @CSRequestParam("name") String name){
            String result=demoService.get(name);
    
            try {
                resp.getWriter().write(result);
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    
        @CSRequestMapping("/add")
        public void add(HttpServletRequest req, HttpServletResponse resp, @CSRequestParam("aa") Integer a,@CSRequestParam("b") Integer b){
            try {
                resp.getWriter().write(a+"+"+b+"="+(a+b));
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    
        @CSRequestMapping("/remove")
        public void remove(HttpServletRequest req, HttpServletResponse resp, @CSRequestParam("id") Integer id){
            try {
                resp.getWriter().write("id="+id);
            } catch (IOException exception) {
                exception.printStackTrace();
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    5.准备servlet

    主要实现了以下功能:
    1).根据@CSController对外服务的url如何mapping到具体方法 doHandlerMap
    2).service和controller bean的管理 iocBeans
    3).如何实列化bean doInstance
    4).如何获取url中参数值 doDispatch中
    5).找到需要加载的class doScanner
    6).如何自动autowired doAutoWried

    public class CSDispatchServlet extends HttpServlet {
    
        public static String urlPattern="/custom";
    
        private void doDispatch(HttpServletRequest request,HttpServletResponse response) throws Exception{
            String url=request.getRequestURI();
            String contextPath=request.getContextPath();
            url=url.replace(urlPattern,"");
    
            if(!handlerMap.containsKey(url)){
                response.getWriter().write("404 not found!");
                return;
            }
    
            Method method=handlerMap.get(url);
            Annotation[][] methodParameterAnnotations= method.getParameterAnnotations();
    
            Parameter[]  methodParameters= method.getParameters();
            Annotation[][] paramerterAnnotations=method.getParameterAnnotations();
    
            ArrayList<Object> methodParameterValues=new  ArrayList<Object>();
    
            Map<String,String[]> requestParams= request.getParameterMap();
    
            int parmeterCnt=0;
    
            for(Parameter parameter:methodParameters){
    
                if(parameter.getType()==HttpServletRequest.class ){
                    methodParameterValues.add(request);
                }else if(parameter.getType()==HttpServletResponse.class){
                    methodParameterValues.add(response);
                }else {
    
                   String methodParamName="";
                   if(paramerterAnnotations[parmeterCnt].length>0) {
                           Annotation annotation= paramerterAnnotations[parmeterCnt][0];
                           if(annotation instanceof CSRequestParam) {
                               methodParamName = ((CSRequestParam) annotation).value();
                           }
                   }
    
    
                   if("".equals(methodParamName.trim())){
                       methodParamName=parameter.getName();
                   }
    
                    String value="";
                   //String value=Arrays.toString(requestParams.get(methodParamName));
                    if(requestParams.get(methodParamName).length>1)
                        value=Arrays.toString(requestParams.get(methodParamName));
                    else if(requestParams.get(methodParamName).length==1)
                         value= requestParams.get(methodParamName)[0];
                    else
                        value="999999";
    
                   if(parameter.getType()==String.class)
                       methodParameterValues.add(value);
                   else if(parameter.getType()==Integer.class) {
                       try {
                           methodParameterValues.add(Integer.parseInt(value));
                       } catch (Exception e){
                           methodParameterValues.add(99999999);
                       }
                   }else {
                        //可以扩展复杂类型转换
                   }
                }
    
                parmeterCnt++;
            }
    
            String beanName=this.genBeanName(method.getDeclaringClass().getSimpleName());
            method.invoke(this.iocBeans.get(beanName), methodParameterValues.toArray());
    
        }
    
        private String genBeanName(String beanName){
            if(beanName.length()>1)
                beanName=beanName.substring(0,0).toLowerCase()+beanName.substring(1);
            else
                beanName=beanName.toLowerCase();
    
            return beanName;
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doPost(req,resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                this.doDispatch(req,resp);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    
        private ArrayList<String> classs=new ArrayList<String>();
        private ConcurrentHashMap<String,Object> iocBeans=new ConcurrentHashMap<String,Object>();
        private ConcurrentHashMap<String,Method> handlerMap=new ConcurrentHashMap<String,Method>();
    
    
        private void doInstance() {
            try {
                for (String className : classs) {
                    if (!className.contains(".")) continue;
    
                    Class<?> clazz = Class.forName(className);
                    String beanName="";
                    if (clazz.isAnnotationPresent(CSController.class)) {
                        CSController controller = clazz.getAnnotation(CSController.class);
                        beanName=controller.value();
                    }else if(clazz.isAnnotationPresent(CSService.class)){
                        CSService service=clazz.getAnnotation(CSService.class);
                        beanName=service.value();
                    }else {
                        continue;
                    }
    
                    Object instance=clazz.newInstance();
    
                    if("".equals(beanName.trim()))
                        beanName=clazz.getSimpleName();            
    
                    beanName=genBeanName(beanName);
                    iocBeans.put(beanName,instance);
    
                    if(clazz.isAnnotationPresent(CSService.class)){
                        for(Class  c:  clazz.getInterfaces()){
                            if(iocBeans.containsKey(c.getName())) continue;
                            iocBeans.put(c.getName(),instance);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        private void doAutoWried(){
            for(Object o:iocBeans.values()){
                if(o==null) continue;
                Class clazz=o.getClass();
                if(clazz.isAnnotationPresent(CSService.class) || clazz.isAnnotationPresent(CSController.class)){
                    Field[] fields=clazz.getDeclaredFields();
                    for(Field f:fields){
                        if(!f.isAnnotationPresent(CSAutowired.class)) continue;
                        CSAutowired autowired=f.getAnnotation(CSAutowired.class);
                        String beanName=autowired.value();
                        if("".equals(beanName)) beanName=f.getType().getName();
                        f.setAccessible(true);
                        try{
                            Object o1=iocBeans.get(beanName);
                            f.set(o,iocBeans.get(beanName));
                        }catch (IllegalAccessException e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
        private void doHandlerMap(ServletConfig config){ 
            for(Object o:iocBeans.values()){
    
                if(!o.getClass().isAnnotationPresent(CSController.class)) continue;
    
                String baseUrl="";
                if(o.getClass().isAnnotationPresent(CSRequestMapping.class)){
                    CSRequestMapping requestMapping=o.getClass().getAnnotation(CSRequestMapping.class);
                    baseUrl=requestMapping.value();
                }
    
                for(Method method: o.getClass().getMethods()){
                    if(method.isAnnotationPresent(CSRequestMapping.class)) {
                        CSRequestMapping requestMapping=method.getAnnotation(CSRequestMapping.class);
                        String url=baseUrl+requestMapping.value().replaceAll("/+","/");
                        String contextPath=config.getServletContext().getContextPath();
    
                        this.handlerMap.put(url,method);
                    }
                }
            }
        }
    
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            InputStream is=null;
            try{
    
                System.out.println("custom servlet init........");
    
                /*
                Properties configContext=new Properties();
                is=this.getClass().getClassLoader().getResourceAsStream(config.getInitParameter("contextConfigLocation"));
                configContext.load(is);
                String scanPackage=configContext.getProperty("scanPackage");
                */
    
                Enumeration<String> enumerations= config.getInitParameterNames();
                while (enumerations.hasMoreElements()){
                    System.out.println(enumerations.nextElement());
                }
    
                doScanner("com.mesui.spring.custom");
                doInstance();
                doAutoWried();
                doHandlerMap( config);
    
    
            }catch (Exception exception){
                exception.printStackTrace();
            }finally {
    
            }
    
        }
    
        private void doScanner(String scanPackage){
    
            URL url= this.getClass().getClassLoader().getResource("") ;
    
            String filePath="";
    
            try {
                  filePath= URLDecoder.decode( url.getPath(),"UTF-8")+"/"+scanPackage.replaceAll("\\.","/");
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
    
            File classDir=new File(filePath);
            for(File file:classDir.listFiles()){
                if(file.isDirectory()){
                    doScanner(scanPackage+"."+file.getName());
                }else if(!file.getName().endsWith(".class")) {
                    continue;
                }
                if(!file.isDirectory()) {
                    String clzzName = (scanPackage + "." + file.getName().replace(".class", ""));
                    //map.put(clzzName,null);
                    classs.add(clzzName);
                }
    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251

    6.在利用spring的configuration类初始化servlet

    这边为了方便进行偷懒,这样/custom/下的服务按照自已逻辑对对外服务,不按照spring mvc的进行,另外自已可以tomcat的web.xml中标记servlet完全脱离spring

        @Configuration
    public class MybatisPlusConfig {
    
        @Bean
        public ServletRegistrationBean CustomServlet(){
            return new ServletRegistrationBean(new CSDispatchServlet(),CSDispatchServlet.urlPattern+"/*");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7.测试

    在这里插入图片描述

    8.结论

    从上面的例子中我们可以看到自已写一个mvc也很方便,不是什么难事,但是这个只是用于学习,毕竟spring是一个体系,我们自已不可能将所有内容重新写一遍,但是自已写着玩有助于对spring mvc和IOC的理解。

  • 相关阅读:
    金仓数据库KStudio使用手册(6. PLSQL调试)
    生成生动3D人头形象
    uniapp中easycom用法详解
    【头歌C语言程序设计】指针及其应用
    存储模块 --- Cache
    基于Hadoop的学习行为数据云存储平台的设计与实现
    工业机械设备如何做好预测性维护
    Latex在同一figure中排版多张图片的方法
    vue中如何使用swiper以及解决swiper初始化过早的问题
    米软科技客户单病种上报量云南省第一
  • 原文地址:https://blog.csdn.net/lin000_0/article/details/127773352