commit a791209f0650184bf2e40cec519336ca31b63d81 Author: Lu-aiLiang <13208981+Lu-aiLiang@user.noreply.gitee.com> Date: Sun Oct 29 19:46:02 2023 +0800 csh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/$PROJECT_FILE$ b/.idea/$PROJECT_FILE$ new file mode 100644 index 0000000..58b7e3e --- /dev/null +++ b/.idea/$PROJECT_FILE$ @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5825257 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/qaplug_profiles.xml b/.idea/qaplug_profiles.xml new file mode 100644 index 0000000..3dfd21f --- /dev/null +++ b/.idea/qaplug_profiles.xml @@ -0,0 +1,465 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4d6b806 --- /dev/null +++ b/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + org.example + lhx-mvc + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + diff --git a/src/main/java/com/lhj/ai/annotations/EnjoyAutowired.java b/src/main/java/com/lhj/ai/annotations/EnjoyAutowired.java new file mode 100644 index 0000000..32b824b --- /dev/null +++ b/src/main/java/com/lhj/ai/annotations/EnjoyAutowired.java @@ -0,0 +1,16 @@ +package com.lhj.ai.annotations; + +import java.lang.annotation.*; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-28 20:37 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface EnjoyAutowired { + String value() default ""; +} diff --git a/src/main/java/com/lhj/ai/annotations/EnjoyController.java b/src/main/java/com/lhj/ai/annotations/EnjoyController.java new file mode 100644 index 0000000..d247528 --- /dev/null +++ b/src/main/java/com/lhj/ai/annotations/EnjoyController.java @@ -0,0 +1,16 @@ +package com.lhj.ai.annotations; + +import java.lang.annotation.*; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-28 20:42 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface EnjoyController { + String value() default ""; +} diff --git a/src/main/java/com/lhj/ai/annotations/EnjoyRequestMapping.java b/src/main/java/com/lhj/ai/annotations/EnjoyRequestMapping.java new file mode 100644 index 0000000..0666881 --- /dev/null +++ b/src/main/java/com/lhj/ai/annotations/EnjoyRequestMapping.java @@ -0,0 +1,16 @@ +package com.lhj.ai.annotations; + +import java.lang.annotation.*; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-28 20:47 + */ +@Target({ElementType.TYPE,ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface EnjoyRequestMapping { + String value() default ""; +} diff --git a/src/main/java/com/lhj/ai/annotations/EnjoyRequestParam.java b/src/main/java/com/lhj/ai/annotations/EnjoyRequestParam.java new file mode 100644 index 0000000..e76c770 --- /dev/null +++ b/src/main/java/com/lhj/ai/annotations/EnjoyRequestParam.java @@ -0,0 +1,16 @@ +package com.lhj.ai.annotations; + +import java.lang.annotation.*; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-28 20:47 + */ +@Target({ElementType.PARAMETER}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface EnjoyRequestParam { + String value() default ""; +} diff --git a/src/main/java/com/lhj/ai/annotations/EnjoyService.java b/src/main/java/com/lhj/ai/annotations/EnjoyService.java new file mode 100644 index 0000000..c2cb563 --- /dev/null +++ b/src/main/java/com/lhj/ai/annotations/EnjoyService.java @@ -0,0 +1,16 @@ +package com.lhj.ai.annotations; + +import java.lang.annotation.*; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-28 20:47 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface EnjoyService { + String value() default ""; +} diff --git a/src/main/java/com/lhj/ai/controller/JamesController.java b/src/main/java/com/lhj/ai/controller/JamesController.java new file mode 100644 index 0000000..9349d59 --- /dev/null +++ b/src/main/java/com/lhj/ai/controller/JamesController.java @@ -0,0 +1,41 @@ +package com.lhj.ai.controller; + +import com.lhj.ai.annotations.EnjoyAutowired; +import com.lhj.ai.annotations.EnjoyController; +import com.lhj.ai.annotations.EnjoyRequestMapping; +import com.lhj.ai.annotations.EnjoyRequestParam; +import com.lhj.ai.service.JamesService; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 8:23 + */ +@EnjoyController +@EnjoyRequestMapping("james") + +public class JamesController { + @EnjoyAutowired("JamesServiceImpl") + private JamesService jamesService; + + @EnjoyRequestMapping("/query") + public void query(HttpServletRequest request, HttpServletResponse response, + @EnjoyRequestParam("name") String name, @EnjoyRequestParam("age") String age) { + + try { + PrintWriter pw = response.getWriter(); + String result = jamesService.query(name, age); + pw.write(result); + }catch (IOException e){ + e.printStackTrace(); + } + } + + +} diff --git a/src/main/java/com/lhj/ai/mapper/JamesMapper.java b/src/main/java/com/lhj/ai/mapper/JamesMapper.java new file mode 100644 index 0000000..5219c43 --- /dev/null +++ b/src/main/java/com/lhj/ai/mapper/JamesMapper.java @@ -0,0 +1,10 @@ +package com.lhj.ai.mapper; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 8:25 + */ +public interface JamesMapper { +} diff --git a/src/main/java/com/lhj/ai/service/JamesService.java b/src/main/java/com/lhj/ai/service/JamesService.java new file mode 100644 index 0000000..19bb189 --- /dev/null +++ b/src/main/java/com/lhj/ai/service/JamesService.java @@ -0,0 +1,11 @@ +package com.lhj.ai.service; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 8:24 + */ +public interface JamesService { + String query (String name,String age); +} diff --git a/src/main/java/com/lhj/ai/service/impl/JamesServiceImpl.java b/src/main/java/com/lhj/ai/service/impl/JamesServiceImpl.java new file mode 100644 index 0000000..2281df0 --- /dev/null +++ b/src/main/java/com/lhj/ai/service/impl/JamesServiceImpl.java @@ -0,0 +1,18 @@ +package com.lhj.ai.service.impl; + +import com.lhj.ai.annotations.EnjoyService; +import com.lhj.ai.service.JamesService; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 8:24 + */ +@EnjoyService("JamesServiceImpl") +public class JamesServiceImpl implements JamesService { + @Override + public String query(String name, String age) { + return "name ==="+name+", age === "+age; + } +} diff --git a/src/main/java/com/lhj/ai/servlet/DispatcherServlet.java b/src/main/java/com/lhj/ai/servlet/DispatcherServlet.java new file mode 100644 index 0000000..7b3e90f --- /dev/null +++ b/src/main/java/com/lhj/ai/servlet/DispatcherServlet.java @@ -0,0 +1,115 @@ +package com.lhj.ai.servlet; + +import com.lhj.ai.annotations.EnjoyController; +import com.lhj.ai.annotations.EnjoyRequestMapping; +import com.lhj.ai.annotations.EnjoyService; + +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.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @version: java version 1.8 + * @Author: Mr Orange + * @description: + * @date: 2023-10-29 9:03 + */ +public class DispatcherServlet extends HttpServlet { + + List classNames = new ArrayList(); + Map beans = new HashMap(); + + private static final long serialVersionUID = 1L; + + public void init(ServletConfig config) { + //IOC + //吧所有的bean扫描 --- 扫描所有的class文件 + scanPackage("com.enjoy"); + + // + doInstance(); + + doIoc(); + } + + //吧service 注入到controller + public void doIoc() { + if (beans.entrySet().size() <= 0) { + System.out.println("没有一个实例化的类"); + } + for (Map.Entry entry : beans.entrySet()) { + Object instance = entry.getValue(); + Class clazz + = instance.getClass(); + Filed[] fileds = clazz.getDeclaredFields(); + } + } + + private void scanPackage(String basePackage) { + URL url = this.getClass().getClassLoader().getResource("/" + basePackage.replaceAll("\\.", "/")); + String fileStr = url.getFile(); + File file = new File(fileStr); + String[] filesStr = file.list(); + + for (String path : filesStr) { + File filePath = new File(fileStr + path); //com.enjoy.james + + if (filePath.isDirectory()) { //真 代表是文件夹 + scanPackage(basePackage + "." + path); + } else { + // 加入 LIST + classNames.add(basePackage + "." + filePath.getName());//com.lhj....xxx.class + } + } + } + + ; + + private void doInstance() { + // + if (classNames.size() <= 0) { + System.out.println("包扫描失败"); + return; + } + //list 的 + for (String className : classNames) { + String cn = className.replace(".class", ""); + try { + Class clazz = Class.forName(cn); + if (clazz.isAnnotationPresent(EnjoyController.class)) { + Object instace = clazz.newInstance(); + EnjoyRequestMapping requestMapping = clazz.getAnnotation(EnjoyRequestMapping.class); + String rmvalue = requestMapping.value(); + beans.put(rmvalue, instace); + } else if (clazz.isAnnotationPresent(EnjoyService.class)) { + EnjoyService service = clazz.getAnnotation(EnjoyService.class); + Object instance = clazz.newInstance(); + beans.put(service.value(), instance); + } else { + + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + @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 { + + } +} diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..7b91622 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + lhx-mvc + + DispatcherServlet + com.lhj.ai.servlet.DispatcherServlet + + + DispatcherServlet + / + + +