" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring手寫(xiě)簡(jiǎn)化版MVC流程詳解

 更新時(shí)間:2022年11月11日 10:00:45   作者:lin000_0  
Spring MVC是Spring Framework的一部分,是基于Java實(shí)現(xiàn)MVC的輕量級(jí)Web框架。本文將通過(guò)簡(jiǎn)單示例帶大家掌握SpringMVC簡(jiǎn)化版手寫(xiě)方法,感興趣的可以了解一下

spring是一個(gè)非常流行的技術(shù)框架,其中spring mvc組件在其中非常重要的地位,主要面要客戶(hù)端提供服務(wù),我們今天來(lái)手寫(xiě)一個(gè)簡(jiǎn)化版的mvc,且包括ioc部分,主要利用servlet機(jī)制來(lái)實(shí)現(xiàn),類(lèi)的關(guān)系如下:

準(zhǔn)備注解類(lèi),類(lèi)于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 "";
}

準(zhǔn)備service interface

public interface IDemoService {
    public String get(String name);
}

準(zhǔn)備service實(shí)現(xiàn)類(lèi),利用@CSService

@CSService
public class DemoService implements IDemoService {
    @Override
    public String get(String name) {
        return "My name is "+name;
    }
}

準(zhǔn)備對(duì)外服務(wù)的類(lèi),主要利用@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();
        }
    }
}

準(zhǔn)備servlet

主要實(shí)現(xiàn)了以下功能:

1).根據(jù)@CSController對(duì)外服務(wù)的url如何mapping到具體方法 doHandlerMap

2).service和controller bean的管理 iocBeans

3).如何實(shí)列化bean doInstance

4).如何獲取url中參數(shù)值 doDispatch中

5).找到需要加載的class doScanner

6).如何自動(dòng)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 {
                    //可以擴(kuò)展復(fù)雜類(lèi)型轉(zhuǎn)換
               }
            }
            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);
            }
        }
    }
}

在利用spring的configuration類(lèi)初始化servlet

這邊為了方便進(jìn)行偷懶,這樣/custom/下的服務(wù)按照自已邏輯對(duì)對(duì)外服務(wù),不按照spring mvc的進(jìn)行,另外自已可以tomcat的web.xml中標(biāo)記servlet完全脫離spring

    @Configuration
public class MybatisPlusConfig {
    @Bean
    public ServletRegistrationBean CustomServlet(){
        return new ServletRegistrationBean(new CSDispatchServlet(),CSDispatchServlet.urlPattern+"/*");
    }
}

測(cè)試

結(jié)論

從上面的例子中我們可以看到自已寫(xiě)一個(gè)mvc也很方便,不是什么難事,但是這個(gè)只是用于學(xué)習(xí),畢竟spring是一個(gè)體系,我們自已不可能將所有內(nèi)容重新寫(xiě)一遍,但是自已寫(xiě)著玩有助于對(duì)spring mvc和IOC的理解。

到此這篇關(guān)于Spring手寫(xiě)簡(jiǎn)化版mvc流程詳解的文章就介紹到這了,更多相關(guān)Spring mvc內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SWT(JFace)體驗(yàn)之StyledText類(lèi)

    SWT(JFace)體驗(yàn)之StyledText類(lèi)

    有的時(shí)候Text需要實(shí)現(xiàn)這種那種的樣式。先提供在不使用StyledText類(lèi)的情況:
    2009-06-06
  • SpringCloud Webflux過(guò)濾器增加header傳遞方式

    SpringCloud Webflux過(guò)濾器增加header傳遞方式

    這篇文章主要介紹了SpringCloud Webflux過(guò)濾器增加header傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • java 按行讀取文件并輸出到控制臺(tái)的方法

    java 按行讀取文件并輸出到控制臺(tái)的方法

    今天小編就為大家分享一篇java 按行讀取文件并輸出到控制臺(tái)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)

    搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)

    這篇文章主要為大家介紹了搜索一文入門(mén)ElasticSearch(節(jié)點(diǎn) 分片 CRUD 倒排索引 分詞)的基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • AndroidHttpClient使用Cookie應(yīng)用分析

    AndroidHttpClient使用Cookie應(yīng)用分析

    今天想把一個(gè)用使用了HttpClient的自動(dòng)簽到小程序移植到Android上,還好Android的SDK自帶了HttpClient的包.當(dāng)然也可以繼續(xù)使用DefaultHttpClient,但用為Android定制的AndroidHttpClient自然更好
    2012-11-11
  • Spring中XmlWebApplicationContext的實(shí)現(xiàn)

    Spring中XmlWebApplicationContext的實(shí)現(xiàn)

    XmlWebApplicationContext是Spring?Framework中的一個(gè)重要類(lèi),本文主要介紹了Spring中XmlWebApplicationContext,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Java基礎(chǔ)-Java的體系結(jié)構(gòu)

    Java基礎(chǔ)-Java的體系結(jié)構(gòu)

    這篇文章主要介紹了Java的體系結(jié)構(gòu),Java幾乎成為了“開(kāi)源”的代名詞。第三方開(kāi)源軟件和框架。如Tomcat、Struts,MyBatis,Spring等,下面我們來(lái)看看文章具體的內(nèi)容介紹吧
    2022-01-01
  • mybatis開(kāi)啟spring事務(wù)代碼解析

    mybatis開(kāi)啟spring事務(wù)代碼解析

    這篇文章主要介紹了mybatis開(kāi)啟spring事務(wù)代碼解析,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Spring BeanFactory和FactoryBean區(qū)別解析

    Spring BeanFactory和FactoryBean區(qū)別解析

    這篇文章主要介紹了Spring BeanFactory和FactoryBean區(qū)別解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟

    使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟

    這篇文章主要介紹了使用sts工具、SpringBoot整合mybatis的詳細(xì)步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04

最新評(píng)論

奉新县| 嫩江县| 郎溪县| 鹿泉市| 密山市| 廊坊市| 共和县| 舟曲县| 墨脱县| 甘孜县| 阿城市| 大丰市| 钟山县| 德格县| 左云县| 阿坝县| 新乡市| 金昌市| 崇文区| 商城县| 遵化市| 泰和县| 如东县| 喀喇沁旗| 合山市| 阜宁县| 丁青县| 观塘区| 彭山县| 灵川县| 前郭尔| 南城县| 广元市| 台山市| 永德县| 吴忠市| 东乌珠穆沁旗| 商水县| 滨海县| 吴桥县| 秦安县|