web.xml中声明此dispatcherServlet, 目的: ①表示这是个会被tomcat容器识别的servlet, ②拦截全部请求html
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringSim2</display-name> <servlet> <servlet-name>springsim2</servlet-name> <servlet-class>com.spring.sim.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springsim2</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
src下建立application.properties, 指定须要被扫描的包java
path=com.spring.sim
web.xml中将properties文件设置为启动时被load web
⚠ 此步骤为可选, 在DispatcherServlet中声明也能够spring
此步完成后, web.xml不须要再改动api
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringSimv1</display-name> <servlet> <servlet-name>springsim</servlet-name> <servlet-class>pro.yizheng.DispatcherServlet</servlet-class> <!-- 指定配置文件 --> <init-param> <param-name>webXmlInitParam-properties</param-name> <param-value>application.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springsim</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
声明要用到的Annotationtomcat
Annotation source code:mvc
@Documented @Retention(RUNTIME) @Target(FIELD) public @interface SSAutowired { String value() default ""; } @Documented @Retention(RUNTIME) @Target({ TYPE}) public @interface SSController { String value() default ""; } @Documented @Retention(RUNTIME) @Target({TYPE,METHOD}) public @interface SSRequestMapping { String value() default ""; } @Documented @Retention(RUNTIME) @Target(PARAMETER) public @interface SSRequestParam { String value() default ""; } @Documented @Retention(RUNTIME) @Target(TYPE) public @interface SSService { String value() default ""; }
编写service类, 目的: 模拟spring组件的功能app
@SSService public class Service { public void get(String name) { System.out.println(name); } }
编写Action, 目的: 模拟Springmvc的action, 实际上模拟的Action已经和SpringMVC的相同了this
@SSController @SSRequestMapping("/demo") public class DemoAction { @SSAutowired private SSDemoService service; @SSRequestMapping("/query") public void query(HttpServletRequest requeust, HttpServletResponse response, @SSRequestParam("name") String name) { String reString = service.get(name); try { response.getWriter().write(reString); } catch (Exception e) { e.printStackTrace(); } } @SSRequestMapping("/add") public void add(HttpServletRequest requeust, HttpServletResponse response, @SSRequestParam("name") String name) { try { response.getWriter().write("this is add"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
DispatcherServlet是整个工程的大脑, 它负责整个容器的的初始化, 资源读取管理和请求分发. url
此工程中, dispatcherServlet分为如下几个部分:
初始化容器
接收并处理请求
看起来彷佛很简单, 但实现起来仍是有一些麻烦的.
下面咱们开始.
而在init方法中, 第一步须要提取出全部的Java类型文件, 每个文件都要对应一个或多个路径, 因此咱们先完成遍历类的方法
private Map<String, Object> javaFiles = new HashMap<>();
// 遍历全部class文件 private void scanJavaFiles(String path) { // 经过application.properties中设定的包名, 找到其下全部的java class文件 File pathDir = new File(this.getClass().getResource(path).getPath()); String[] pathList = pathDir.list(); for (String temFile : pathList) { File tempFile = new File(pathDir+ "/" + temFile); // 若是是目录的话, 即递归操做 if (tempFile.isDirectory()) { scanJavaFiles(path + "/" + temFile); } // 非目录, 获得子节点,将其放到全局变量中 if (temFile.endsWith(".class")) { javaFiles.put(path+ "/" + temFile.replace(".class", ""), null); } } }
编写初始化方法 - 此方法名没法更改, 只能是init方法, 这是Servlet规范
public void init(ServletConfig config) throws ServletException { System.out.println("init"); // 获取配置文件中的scan package InputStream is = this.getClass().getClassLoader().getResourceAsStream(config.getInitParameter("param1")); Properties configContext = new Properties(); try { configContext.load(is); } catch (IOException e) { e.printStackTrace(); } String scanPackage = configContext.getProperty("path"); // 获取路径下的java文件 scanJavaFiles(scanPackage); // 第二部分完成 }