diff --git a/Spring-ch09/src/main/java/com/ycjy/MyInit.java b/Spring-ch09/src/main/java/com/ycjy/MyInit.java index e49d69b..03005ae 100644 --- a/Spring-ch09/src/main/java/com/ycjy/MyInit.java +++ b/Spring-ch09/src/main/java/com/ycjy/MyInit.java @@ -9,7 +9,7 @@ import jakarta.servlet.annotation.HandlesTypes; import java.lang.reflect.InvocationTargetException; import java.util.Set; -@HandlesTypes(MyAppInit.class) //告诉容器"把所有实现 MyAppInit 的类塞进 set" +//@HandlesTypes(MyAppInit.class) //告诉容器"把所有实现 MyAppInit 的类塞进 set" public class MyInit implements ServletContainerInitializer { @Override public void onStartup(Set> set, ServletContext ctx) throws ServletException { diff --git a/Spring-ch09/src/main/java/com/ycjy/MyMvcAppInit.java b/Spring-ch09/src/main/java/com/ycjy/MyMvcAppInit.java index 4ba096c..2f2526b 100644 --- a/Spring-ch09/src/main/java/com/ycjy/MyMvcAppInit.java +++ b/Spring-ch09/src/main/java/com/ycjy/MyMvcAppInit.java @@ -1,20 +1,40 @@ package com.ycjy; -import jakarta.servlet.ServletContext; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRegistration; -import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; +import org.jspecify.annotations.Nullable; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; -public class MyMvcAppInit implements WebApplicationInitializer { +//public class MyMvcAppInit implements WebApplicationInitializer { +// @Override +// public void onStartup(ServletContext servletContext) throws ServletException { +// AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); +// context.setServletContext(servletContext); +// context.register(MySystemConfig.class); +// +// ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); +// dispatcher.addMapping("/"); +// } +//} + +// 上面的都是模板方法, 但只有 context.register(配置类); 的配置类是不确定的 +// 所以Spring帮我们做好了 +// 父子 +public class MyMvcAppInit extends AbstractAnnotationConfigDispatcherServletInitializer { + // 根容器, 由ContextLoaderListener创建 @Override - public void onStartup(ServletContext servletContext) throws ServletException { - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.setServletContext(servletContext); - context.register(MySystemConfig.class); - - ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); - dispatcher.addMapping("/"); + protected Class @Nullable [] getRootConfigClasses() { + return null; // 返回空, 不建立根容器 } -} + + // servlet容器, 由DispatcherServlet创建 + // 子容器能看见父容器里的bean, 但父容器看不到子容器里的 + // 找Bean首先会到子容器找, 子容器找不到Bean就去父容器里找 + @Override + protected Class @Nullable [] getServletConfigClasses() { + return new Class[]{MySystemConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} \ No newline at end of file