2个淘宝可以做情侣网站么,电商网站建设方式,wordpress文章迁移,网站角色权限什么是Spring MVC#xff1f; 
Spring Web MVC是基于Servlet API构建的原始Web框架#xff0c;从一开始就包含在Spring Framework中。正式名称“Spring Web MVC”来自其源模块的名称#xff08; spring-webmvc #xff09;#xff0c;但它通常被称为“Spring MVC”。 
手写…什么是Spring MVC 
Spring Web MVC是基于Servlet API构建的原始Web框架从一开始就包含在Spring Framework中。正式名称“Spring Web MVC”来自其源模块的名称 spring-webmvc 但它通常被称为“Spring MVC”。 
手写Spring MVC一 
创建项目 
创建父工程 [ 选择9号模板 site-simple]命名为shop以商城为例子 
创建子工程 [ 选择10号模板webapp]命名为shop-web 
创建子工程 [ 选择7号模板quickstart]命名为shop-mvc 
依赖准备 
servlet-api 
dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion4.0.1/versionscopeprovided/scope
/dependencylombok 
dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.24/version
/dependency编译插件 
buildfinalNameshop_web/finalNamepluginManagement!-- lock down plugins versions to avoid using Maven defaults (maybe moved to parent pom) --plugins!--这个插件就是java类生成class的编译插件--pluginartifactIdmaven-compiler-plugin/artifactIdconfiguration!--编译参数--compilerArgument-parameters/compilerArgumentsource1.8/sourcetarget1.8/target/configuration/plugin/plugins/pluginManagement
/build创建文件 在shop-web工程下创建以下目录   在mvc工程下创建以下目录   
文件详解 
mvc工程 cn.cnmd.shop.mvc.annotation主要是注解包括控制器、路由映射、配置 cn.cnmd.shop.mvc.annotation.Controller package cn.cnmd.shop.mvc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface Controller {
}cn.cnmd.shop.mvc.annotation.RequestMapping package cn.cnmd.shop.mvc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.TYPE, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface RequestMapping {String value();
}cn.cnmd.shop.mvc.annotation.Configuration package cn.cnmd.shop.mvc.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;Target({ElementType.TYPE})
Retention(RetentionPolicy.RUNTIME)
public interface Configuration {String value();
}cn.cnmd.shop.mvc.canstant包含了常见的错误码和错误信息  cn.cnmd.shop.mvc.canstant.ResponseCodeInterface package cn.cnmd.shop.mvc.constant;public interface ResponseCodeInterface {int getCode();String getMessage();void setCode(int code);void setMessage(String message);
}cn.cnmd.shop.mvc.canstant.ResponseCode package cn.cnmd.shop.mvc.constant;public enum ResponseCode implements ResponseCodeInterface {CONFIG_EXCEPTION(100, config的配置信息出错),CONFIGURATION_EXCEPTION(101, 需要配置Configuration这个注解),CLASS_FILE_EXCEPTION(102, class文件转换异常),REQUEST_MAPPING_PATH_EXCEPTION(103, RequestMapping地址设置有误),REQUEST_PATH_EXCEPTION(104, uri映射错误),EXCEPTION_CONFIG_EXCEPTION(105, 未配置全局异常的路径),ADVISER_CONFIG_EXCEPTION(106, 未配置处理器的路径);private int code;private String message;ResponseCode(int code, String message) {this.code  code;this.message  message;}Overridepublic int getCode() {return code;}Overridepublic String getMessage() {return message;}Overridepublic void setCode(int code) {this.code  code;}Overridepublic void setMessage(String message) {this.message  message;}
}   cn.cnmd.shop.mvc.container容器用于存储项目启动之后创建的BeanDefinition对象  cn.cnmd.shop.mvc.container.BeanContainer package cn.cnmd.shop.mvc.container;import cn.cnmd.shop.mvc.model.BeanDefinition;
import lombok.Getter;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;public class BeanContainer {Getterprivate static MapString, BeanDefinition? maps  null;static {maps  new ConcurrentHashMap();}
}   cn.cnmd.shop.mvc.exception异常类用于处理异常  cn.cnmd.shop.mvc.exception.FrameWorkException package cn.cnmd.shop.mvc.exception;import lombok.AllArgsConstructor;
import lombok.Data;AllArgsConstructor
Datapublic class FrameWorkException extends RuntimeException {private int code;private String message;
}cn.cnmd.shop.mvc.listener监听器主要作用是在项目启动时扫描controller下的文件生成BeanDefinition对象  cn.cnmd.shop.mvc.listener.ApplicationListener package cn.cnmd.shop.mvc.listener;import cn.cnmd.shop.mvc.annotation.Configuration;
import cn.cnmd.shop.mvc.annotation.RequestMapping;
import cn.cnmd.shop.mvc.constant.ResponseCode;
import cn.cnmd.shop.mvc.container.BeanContainer;
import cn.cnmd.shop.mvc.exception.FrameWorkException;
import cn.cnmd.shop.mvc.model.BeanDefinition;
import cn.cnmd.shop.mvc.model.MethodDefinition;
import cn.cnmd.shop.mvc.model.ParameterDefinition;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class ApplicationListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {/** 扫描web项目的Controller层* 封装成类描述类的对象* 添加到容器中 -- Key(父级uri子级uri) Value(类描述类的对象)*/ServletContext servletContext  sce.getServletContext();String config  servletContext.getInitParameter(config);//cn.cnmd.config.AppConfigif (config  null) {throw new FrameWorkException(ResponseCode.CONFIG_EXCEPTION.getCode(), ResponseCode.CONFIG_EXCEPTION.getMessage());}//获取配置类的class对象Class? configurationClass  getConfiguration(config);//通过配置类的class对象拿到 AppConfig注解 中的cn.cnmd.controllerString controllerPosition  getControllerPosition(configurationClass);//D:\Desktop\code\apache-tomcat-8.0.49\webapps\ROOT\WEB-INF\classes\cn\cnmd\controllerString controllerAbsolutePath  getControllerAbsolutePath(servletContext, controllerPosition);//获取controller文件夹下所有的文件ListFile fileList  new ArrayList();findFileByPath(fileList, controllerAbsolutePath);//文件对象集合 -- class对象集合ListClass? classes  transformTo(servletContext, fileList);//封装成类描述类的对象: BeanDefinition - MethodDefinition - ParameterDefinitionhandleController(classes);}/*** 通过web.xml的配置* context-param* param-nameconfig/param-name* param-valuecn.cnmd.config.AppConfig/param-value* /context-param* 参数 [config] 获取的 [cn.cnmd.config.AppConfig] 获取配置文件类的class对象** param config [cn.cnmd.config.AppConfig]* return class对象*/public Class? getConfiguration(String config) {try {return Class.forName(config);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}/*** 通过配置文件类的class对象获取注解中的controller的类的全限定名 [cn.cnmd.controller]** param configurationClass 配置文件类的class对象* return controller的类的全限定名*/public String getControllerPosition(Class? configurationClass) {Configuration annotation  configurationClass.getAnnotation(Configuration.class);if (annotation  null) {throw new FrameWorkException(ResponseCode.CONFIGURATION_EXCEPTION.getCode(), ResponseCode.CONFIGURATION_EXCEPTION.getMessage());}return annotation.value();}/*** 通过配置文件类 [AppConfig] 的注解信息 [cn.cnmd.controller] 获取controller包的发布路径** param servletContext     servlet上下文对象* param controllerPosition 配置文件类 [AppConfig] 的注解信息 [cn.cnmd.controller]* return controller包的发布路径*/public String getControllerAbsolutePath(ServletContext servletContext, String controllerPosition) {//D:\Desktop\code\apache-tomcat-8.0.49\webapps\ROOT\WEB-INF\classesString absolutePath  servletContext.getRealPath(WEB-INF  File.separator  classes);controllerPosition  controllerPosition.replace(., File.separator);return absolutePath  File.separator  controllerPosition;}/*** 通过controller包路径找到路径下所有文件对象** param fileList               文件对象集合* param controllerAbsolutePath controller包的发布路径*/public void findFileByPath(ListFile fileList, String controllerAbsolutePath) {File file  new File(controllerAbsolutePath);File[] files  file.listFiles();if (files ! null) {for (File f : files) {if (f.isDirectory()) {findFileByPath(fileList, f.getAbsolutePath());} else if (f.isFile()) {fileList.add(f);}}}}public ListClass? transformTo(ServletContext servletContext, ListFile fileList) {ListClass? classes  new ArrayList();for (File file : fileList) {//D:\Desktop\code\apache-tomcat-8.0.49\webapps\ROOT\WEB-INF\classes\cn\cnmd\controller\UserController.classString classPath  file.getAbsolutePath();String absolutePath  servletContext.getRealPath(WEB-INF  File.separator  classes);//cn\cnmd\controller\UserController.class -- cn.cnmd.controller.UserControllerclassPath  classPath.substring(absolutePath.length()  1).split(\\.)[0].replace(\\, .);System.out.println(classPath);Class? clazz  null;try {clazz  Class.forName(classPath);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}classes.add(clazz);}return classes;}public void handleController(ListClass? classes) {MapString, BeanDefinition? maps  BeanContainer.getMaps();for (Class? clazz : classes) {try {//父级url requestMappingPath -- requestMapping.value()RequestMapping requestMapping  clazz.getAnnotation(RequestMapping.class);String requestMappingPath1  requestMapping.value();//类名 beanName -- clazz.getName()String beanName  clazz.getName();//controller的类对象 t -- clazz.newInstance()Object t  clazz.newInstance();//方法描述对象Method[] methods  clazz.getMethods();for (Method method : methods) {method.setAccessible(true);if (!method.isAnnotationPresent(RequestMapping.class)) {continue;}RequestMapping requestMapping1  method.getAnnotation(RequestMapping.class);//子级url requestMappingPath -- requestMapping1.value()String requestMappingPath2  requestMapping1.value();//方法名 methodName -- method.getName()String methodName  method.getName();//方法对象 method -- method//返回值类型 returnType --  method.getReturnType()Class? returnType  method.getReturnType();//参数描述对象Parameter[] parameters  method.getParameters();ListParameterDefinition parameterDefinitions  new ArrayList();for (int i  0; i  parameters.length; i) {Class? extends Parameter paramClass  parameters[i].getClass();//参数名 parameterName -- paramClass.getName()String parameterName  paramClass.getName();//参数类型 type -- paramClass//参数下标 index -- iParameterDefinition parameterDefinition  new ParameterDefinition(parameterName, paramClass, i);parameterDefinitions.add(parameterDefinition);}MethodDefinition methodDefinition  new MethodDefinition(requestMappingPath2, methodName, method, returnType, parameterDefinitions);BeanDefinition? beanDefinition  new BeanDefinition(requestMappingPath1, beanName, clazz, t, methodDefinition);String route  requestMappingPath1  File.separator  requestMappingPath2;maps.put(route, beanDefinition);}} catch (InstantiationException | IllegalAccessException e) {throw new RuntimeException(e);}}}
}   cn.cnmd.shop.mvc.model类描述类包括参数描述类、方法描述类、类描述类  cn.cnmd.shop.mvc.model.ParameterDefinition package cn.cnmd.shop.mvc.model;import com.sun.istack.internal.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 参数描述类*/NoArgsConstructor
AllArgsConstructor
Data
public class ParameterDefinition {private String name;//参数名private Class? type;//参数类型private int index;//参数下标
}  cn.cnmd.shop.mvc.model.MethodDefinition package cn.cnmd.shop.mvc.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.lang.reflect.Method;
import java.util.List;NoArgsConstructor
AllArgsConstructor
Data
public class MethodDefinition {private String requestMappingPath;//子级urlprivate String methodName;//方法名private Method method;//方法对象private Class? returnType;//返回值类型private ListParameterDefinition parameters;//参数描述列表
}  cn.cnmd.shop.mvc.model.BeanDefinition package cn.cnmd.shop.mvc.model;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;NoArgsConstructor
AllArgsConstructor
Data
public class BeanDefinitionT {private String requestMappingPath;//父级urlprivate String name;//类名private Class? beanClass;//controller类的class对象private T t;//controller类对象private MethodDefinition methodDefinition;//方法描述对象
}web工程 cn.cnmd.config  cn.cnmd.config.AppConfig package cn.cnmd.config;import cn.cnmd.shop.mvc.annotation.Configuration;Configuration(cn.cnmd.controller)
public class AppConfig {}cn.cnmd.controller存放各种controller  cn.cnmd.controller.UserController package cn.cnmd.controller;import cn.cnmd.pojo.User;
import cn.cnmd.shop.mvc.annotation.Controller;
import cn.cnmd.shop.mvc.annotation.RequestMapping;Controller
RequestMapping(user)
public class UserController {RequestMapping(login1)public void login(String username, String password) {}RequestMapping(login2)public void login(User user) {}public void method(String name){}
}cn.cnmd.controller.back.AdminController package cn.cnmd.controller.back;import cn.cnmd.shop.mvc.annotation.Controller;
import cn.cnmd.shop.mvc.annotation.RequestMapping;Controller
RequestMapping(admin)
public class AdminController {RequestMapping(login)public void login(String name, String password){}
}   cn.cnmd.pojo用于存放web项目中的JavaBean对象  cn.cnmd.pojo.User package cn.cnmd.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;NoArgsConstructor
AllArgsConstructor
Data
public class User {String name;int age;String sex;
}ApplicationContextListener扫描过程个人理解 roller RequestMapping(“admin”) public class AdminController { RequestMapping(login)public void login(String name, String password){}}cn.cnmd.pojo用于存放web项目中的JavaBean对象  cn.cnmd.pojo.User package cn.cnmd.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;NoArgsConstructor
AllArgsConstructor
Data
public class User {String name;int age;String sex;
}ApplicationContextListener扫描过程个人理解 
[外链图片转存中…(img-wN91i1Ja-1719492618962)] 
 文章转载自: http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.baohum.com.gov.cn.baohum.com http://www.morning.ffydh.cn.gov.cn.ffydh.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.jwfkk.cn.gov.cn.jwfkk.cn http://www.morning.jgncd.cn.gov.cn.jgncd.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.sbncr.cn.gov.cn.sbncr.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.kczkq.cn.gov.cn.kczkq.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.glkhx.cn.gov.cn.glkhx.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.rfxw.cn.gov.cn.rfxw.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.lmhwm.cn.gov.cn.lmhwm.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.incmt.com.gov.cn.incmt.com http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.chkfp.cn.gov.cn.chkfp.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.kbqbx.cn.gov.cn.kbqbx.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn