改用 AbstractAnnotationConfigDispatcherServletInitializer 简化初始化
This commit is contained in:
@@ -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<Class<?>> set, ServletContext ctx) throws ServletException {
|
||||
|
||||
@@ -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[]{"/"};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user