用 @HandlesTypes 自动发现并调度 Servlet 注册器
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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<Class<?>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user