续接前文,

手写SpringMVC框架(一)项目搭建

本节我们来开始手写SpringMVC框架的第二阶段:结构开发设计。

新建一个空的springmvc.properties,

里面写我们要扫描的包名,之后这里面我们需要些一些配置信息,比如扫描的包路径等等。

scanPackage=com.lagou.edu

在web.xml中配置此文件:

<servlet>
<servlet-name>lgmvc</servlet-name>
<servlet-class>com.lagou.edu.mvcframework.servlet.LgDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.properties</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>lgmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

servlet中真正处理的几个方法的建立:

package com.lagou.edu.mvcframework.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* @author liuyj
* @Title: LgDispatcherServlet
* @create 2020-06-11 14:22
* @ProjectName springmvc-demo
* @Description: TODO
*/
public class LgDispatcherServlet extends HttpServlet { @Override
public void init(ServletConfig config) throws ServletException { //1.加载springmvc.properties
String contextConfigLocation = config.getInitParameter("contextConfigLocation");
doLoadConfig(contextConfigLocation); //2.扫描相关的类,扫描注解
doScan(); //3.初始化bean(实现IOC容器,基于注解)
doInstance(); //4.实现依赖注入
doAutoWired(); //5.构造一个HandleMapping处理器映射器,将配置好的url和Method建立映射关系
initHandleMapping();
} private void initHandleMapping() { } private void doAutoWired() { } private void doInstance() { } private void doScan() { } //加载配置文件
private void doLoadConfig(String contextConfigLocation) {
} @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 {
//真正处理请求 } }

本节到此结束。

下一节我们来开始具体方法的实现。

欢迎访问:

微信公众号(程序员资料站):code_data

05-11 08:53