From 187bf892569f08c188de8f9747713489160145d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=9C=96=E9=BA=92?= Date: Wed, 21 Oct 2026 20:08:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=20@HandlesTypes=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8F=91=E7=8E=B0=E5=B9=B6=E8=B0=83=E5=BA=A6=20Servlet=20?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ycjy/Impl/MyAppInitImpl1.java | 23 ++++++++++++++++++ .../java/com/ycjy/Impl/MyAppInitImpl2.java | 16 +++++++++++++ .../src/main/java/com/ycjy/MyInit.java | 24 ++++++++++--------- .../java/com/ycjy/interfaces/MyAppInit.java | 8 +++++++ .../java/com/ycjy/servlet/ThreeServlet.java | 15 ++++++++++++ 5 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl1.java create mode 100644 Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl2.java create mode 100644 Spring-ch09/src/main/java/com/ycjy/interfaces/MyAppInit.java create mode 100644 Spring-ch09/src/main/java/com/ycjy/servlet/ThreeServlet.java diff --git a/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl1.java b/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl1.java new file mode 100644 index 0000000..c638894 --- /dev/null +++ b/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl1.java @@ -0,0 +1,23 @@ +package com.ycjy.Impl; + +import com.ycjy.interfaces.MyAppInit; +import com.ycjy.servlet.FirstServlet; +import com.ycjy.servlet.SecondServlet; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRegistration; + +public class MyAppInitImpl1 implements MyAppInit { + @Override + public void onStartup(ServletContext ctx) throws ServletException { + // new Servlet, 一般情况下老师都教不能new, 但是特殊情况下可以 + FirstServlet fs = new FirstServlet(); + SecondServlet ss = new SecondServlet(); + + // 注册并映射动态代理 + ServletRegistration.Dynamic a1 = ctx.addServlet("a1", fs); + a1.addMapping("/a"); + ServletRegistration.Dynamic a2 = ctx.addServlet("a2", ss); + a2.addMapping("/b"); + } +} \ No newline at end of file diff --git a/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl2.java b/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl2.java new file mode 100644 index 0000000..b77e8a1 --- /dev/null +++ b/Spring-ch09/src/main/java/com/ycjy/Impl/MyAppInitImpl2.java @@ -0,0 +1,16 @@ +package com.ycjy.Impl; + +import com.ycjy.interfaces.MyAppInit; +import com.ycjy.servlet.ThreeServlet; +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRegistration; + +public class MyAppInitImpl2 implements MyAppInit { + @Override + public void onStartup(ServletContext ctx) throws ServletException { + ThreeServlet ts = new ThreeServlet(); + ServletRegistration.Dynamic ts1 = ctx.addServlet("ts", ts); + ts1.addMapping("/c"); + } +} diff --git a/Spring-ch09/src/main/java/com/ycjy/MyInit.java b/Spring-ch09/src/main/java/com/ycjy/MyInit.java index 0decb99..e49d69b 100644 --- a/Spring-ch09/src/main/java/com/ycjy/MyInit.java +++ b/Spring-ch09/src/main/java/com/ycjy/MyInit.java @@ -1,25 +1,27 @@ package com.ycjy; -import com.ycjy.servlet.FirstServlet; -import com.ycjy.servlet.SecondServlet; +import com.ycjy.interfaces.MyAppInit; import jakarta.servlet.ServletContainerInitializer; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRegistration; +import jakarta.servlet.annotation.HandlesTypes; +import java.lang.reflect.InvocationTargetException; import java.util.Set; +@HandlesTypes(MyAppInit.class) //告诉容器"把所有实现 MyAppInit 的类塞进 set" public class MyInit implements ServletContainerInitializer { @Override public void onStartup(Set> set, ServletContext ctx) throws ServletException { - // new Servlet, 一般情况下老师都教不能new, 但是特殊情况下可以 - FirstServlet fs = new FirstServlet(); - SecondServlet ss = new SecondServlet(); + if(set == null) return; - // 注册并映射动态代理 - ServletRegistration.Dynamic a1 = ctx.addServlet("a1", fs); - a1.addMapping("/a"); - ServletRegistration.Dynamic a2 = ctx.addServlet("a2", ss); - a2.addMapping("/b"); + for (Class aClass : set) { + try { + MyAppInit myAppInit = (MyAppInit) aClass.getDeclaredConstructor().newInstance(); + myAppInit.onStartup(ctx); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + throw new RuntimeException(e); + } + } } } diff --git a/Spring-ch09/src/main/java/com/ycjy/interfaces/MyAppInit.java b/Spring-ch09/src/main/java/com/ycjy/interfaces/MyAppInit.java new file mode 100644 index 0000000..0174a7a --- /dev/null +++ b/Spring-ch09/src/main/java/com/ycjy/interfaces/MyAppInit.java @@ -0,0 +1,8 @@ +package com.ycjy.interfaces; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.ServletException; + +public interface MyAppInit { + void onStartup(ServletContext ctx) throws ServletException; +} \ No newline at end of file diff --git a/Spring-ch09/src/main/java/com/ycjy/servlet/ThreeServlet.java b/Spring-ch09/src/main/java/com/ycjy/servlet/ThreeServlet.java new file mode 100644 index 0000000..1b87177 --- /dev/null +++ b/Spring-ch09/src/main/java/com/ycjy/servlet/ThreeServlet.java @@ -0,0 +1,15 @@ +package com.ycjy.servlet; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +public class ThreeServlet extends HttpServlet { + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.getWriter().print("ThreeServlet"); + } +}