简化版SpringMVC

2024-02-08 07:20

本文主要是介绍简化版SpringMVC,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简化版SpringMVC

web.xml

xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Web Application</display-name>

    <servlet>

       <servlet-name>mvc</servlet-name>

       <servlet-class>com.aop.mvc.action.DispatcherServlet</servlet-class>

       <init-param>

           <param-name>contextConfigLocation</param-name>

           <param-value>application.properties</param-value>

       </init-param>

       <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

       <servlet-name>mvc</servlet-name>

       <url-pattern>/*</url-pattern>

    </servlet-mapping>

</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aop.demo</groupId>

    <artifactId>SpringAop-Demo</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>SpringAop-Demo</name>

    <url>http://maven.apache.org</url>

    <properties>

       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>

       <dependency>

           <groupId>junit</groupId>

           <artifactId>junit</artifactId>

           <version>3.8.1</version>

           <scope>test</scope>

       </dependency>

       <dependency>

           <groupId>javax.servlet</groupId>

           <artifactId>servlet-api</artifactId>

           <version>2.4</version>

           <scope>provided</scope>

       </dependency>

    </dependencies>

    <build>

       <finalName>${artifactId}</finalName>

       <resources>

           <resource>

              <directory>${basedir}/src/main/resources</directory>

              <includes>

                  <include>**/*</include>

              </includes>

           </resource>

           <resource>

              <directory>${basedir}/src/main/java</directory>

              <excludes>

                  <exclude>**/*.java</exclude>

                  <exclude>**/*.class</exclude>

              </excludes>

           </resource>

       </resources>

    </build>

</project>

application.properties

scanPackage=com.aop.mvc

com.aop.mvc.annotaion

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target({ ElementType.FIELD })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface AutoWirted {

String value() default "";

}

@Target({ ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Controller {

String value() default "";

}

@Target({ ElementType.TYPE, ElementType.METHOD })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface RequestMapping {

String value() default "";

}

@Target({ ElementType.PARAMETER })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface RequestParam {

String value() default "";

}

@Target({ ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Service {

String value() default "";

}

DemoService.java

package com.aop.mvc.servlet.impl;

import com.aop.mvc.annotaion.Service;

import com.aop.mvc.servlet.IDemoService;

@Service

public class DemoService implements IDemoService {

    public String get(String name) {

       return "My name is " + name;

    }

}

package com.aop.mvc.servlet;

public interface IDemoService {

String get(String name);

}

DispatcherServlet.java

package com.aop.mvc.action;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

import java.net.URL;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Properties;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.aop.mvc.annotaion.AutoWirted;

import com.aop.mvc.annotaion.Controller;

import com.aop.mvc.annotaion.RequestMapping;

import com.aop.mvc.annotaion.Service;

public class DispatcherServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /** 资源文件 */

    private Properties contextConfig = new Properties();

    /** 扫描到的所有类 */

    private List<String> classNames = new ArrayList<String>();

    /** 实例化类集合 */

    private Map<String, Object> ioc = new HashMap<String, Object>();

    /** url ==> 优化:List handlerMapping */

    private Map<String, Method> handlerMapping = new HashMap<String, Method>();

    @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 {

           // 6.等待请求

           doDispatch(req, resp);

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

    private void doDispatch(HttpServletRequest req, HttpServletResponse resp) throws Exception {

       String url = req.getRequestURI();

       String contextPath = req.getContextPath();

       url = url.replace(contextPath, "").replaceAll("/+", "/");

       // 判读HandlerMapping中有返回调用, 没有就404

       if (!handlerMapping.containsKey(url)) {

           resp.getWriter().write("404 Not Found!");

           return;

       }

       Method me = handlerMapping.get(url);

       // me.invoke(req.getParameterMap(), ???);

       System.out.println(me);

    }

    @Override

    public void init(ServletConfig config) throws ServletException {

    System.out.println("======================Begin========================");

       // 1.加载配置文件

       doLoadConfig(config.getInitParameter("contextConfigLocation"));

       // 2.扫描到所有的相关类

       doScanner(contextConfig.getProperty("scanPackage"));

       // 3.初始化刚刚扫描到的类

       doInstance();

       // 4.实现依赖注入

       doAutowired();

       // 5.初始化HadlerMapping

       initHandlerMapping();

       System.out.println("=============== Spring is init OK!");

    }

    private void initHandlerMapping() {

       if (ioc.isEmpty())

           return;

       // 对Controller进行处理

       for (Entry<String, Object> entry : ioc.entrySet()) {

           Class clazz = entry.getValue().getClass();

           // 判断有没有加@Controller注解的

           if (!clazz.isAnnotationPresent(Controller.class))

              continue;

           String baseUtl = "";

           if (clazz.isAnnotationPresent(RequestMapping.class)) {

              RequestMapping requestParam = clazz.getAnnotation(RequestMapping.class);

              baseUtl = requestParam.value();

           }

           Method[] method = clazz.getMethods();

           for (Method meth : method) {

              if (meth.isAnnotationPresent(RequestMapping.class)) {

                  RequestMapping requestParam = meth.getAnnotation(RequestMapping.class);

                  String url = requestParam.value();

                  url = (baseUtl + url);

                  handlerMapping.put(url, meth);

                  System.out.println("Mapped:" + url + "," + method);

              }

           }

       }

    }

    private void doAutowired() {

       if (ioc.isEmpty())

           return;

       for (Entry<String, Object> entry : ioc.entrySet()) {

           Field[] fields = entry.getValue().getClass().getDeclaredFields();

           for (Field field : fields) {

              // 授权, 只要加了Autowired的

              if (!field.isAnnotationPresent(AutoWirted.class))

                  continue;

              AutoWirted autoWirted = field.getAnnotation(AutoWirted.class);

              String beanName = autoWirted.value().trim();

              if ("".equals(beanName)) {

                  beanName = field.getType().getName();

              }

              // 执行强制授权

              field.setAccessible(true);

              // 进行赋值操作

              try {

                  field.set(entry.getValue(), ioc.get(beanName));

              } catch (Exception e) {

                  e.printStackTrace();

                  continue;

              }

           }

       }

    }

    private void doInstance() {

       // 将扫描到的类初始化

       if (classNames.isEmpty())

           return;

       // 通过反射初始化

       try {

           for (String className : classNames) {

              Class clazz = Class.forName(className);

              // 有注解的类初始化

              if (clazz.isAnnotationPresent(Controller.class)) {

                  Object obj = clazz.newInstance();

                  // 初始化后放入ioc容器中

                  // 默认是类名的首字母小写

                  String beanName = lowerFirstCase(clazz.getSimpleName());

                  ioc.put(beanName, obj);

              } else if (clazz.isAnnotationPresent(Service.class)) {

                  // 1.默认首字母小写

                  // 2.如果是接口, 要把他的实现类赋值给它

                  // 3.如果自定义,就优先用自定义的名字

                  Service service = clazz.getAnnotation(Service.class);

                  String beanName = service.value();

                  if ("".equals(beanName.trim())) {

                     // 如果自定义名字为空,取默认值

                     beanName = lowerFirstCase(clazz.getSimpleName());

                  }

                  Object instance = clazz.newInstance();

                  ioc.put(beanName, instance);

                  // 解决子类引用赋值父类的问题

                  Class[] interfaces = clazz.getInterfaces();

                  for (Class i : interfaces) {

                     ioc.put(i.getName(), instance);

                  }

              } else {

                  continue;

              }

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

    private void doScanner(String scanPackage) {

       URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll("\\.", "/"));

       File classDir = new File(url.getFile());

       // 递归扫描指定文件下的类

       for (File file : classDir.listFiles()) {

           if (file.isDirectory()) {// 判断是否文件夹

              doScanner(scanPackage + "." + file.getName());

           } else {// 如果不是文件夹

              String className = scanPackage + "." + file.getName().replace(".class", "");

              classNames.add(className);

           }

       }

    }

    private void doLoadConfig(String contextConfigLocation) {

       InputStream is = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);

       try {

           contextConfig.load(is);

       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           if (null != is) {

              try {

                  is.close();

              } catch (IOException e) {

                  e.printStackTrace();

              }

           }

       }

    }

    /** 自定义首字母小写 */

    private String lowerFirstCase(String str) {

       char[] chars = str.toCharArray();

       chars[0] += 32;

       return String.valueOf(chars);

    }

}

DemoAction.java

package com.aop.mvc.action;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.aop.mvc.annotaion.AutoWirted;

import com.aop.mvc.annotaion.Controller;

import com.aop.mvc.annotaion.RequestMapping;

import com.aop.mvc.annotaion.RequestParam;

import com.aop.mvc.servlet.IDemoService;

/**

 * @Theme 纯手写实现SpringMVC

 * @author http://localhost:8080/SpringAop-Demo/demo/query.json

 */

@Controller

@RequestMapping("/demo")

public class DemoAction {

    @AutoWirted

    private IDemoService demoService;

    @RequestMapping("/query.json")

    public void query(HttpServletRequest req, HttpServletResponse resp, @RequestParam("name") String name) {

       String result = demoService.get(name);

       try {

           resp.getWriter().write(result);

       } catch (Exception e) {

           // TODO: handle exception

       }

    }

    @RequestMapping("/add.json")

    public void add(HttpServletRequest req, HttpServletResponse resp, @RequestParam("a") Integer a, @RequestParam("b") Integer b) {

       try {

           resp.getWriter().write(a + "+" + b + "=" + (a + b));

       } catch (Exception e) {

           // TODO: handle exception

       }

    }

    @RequestMapping("/remove.json")

    public void remove(HttpServletRequest req, HttpServletResponse resp, @RequestParam("id") Integer id) {

       try {

       } catch (Exception e) {

           // TODO: handle exception

       }

    }

}

这篇关于简化版SpringMVC的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/690302

相关文章

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问