Files
Spring-Framework-study/Spring-RequestMapping/src/main/java/com/ycjy/MyInit.java
T
2026-10-23 11:30:29 +08:00

24 lines
1.0 KiB
Java

package com.ycjy;
import com.ycjy.confing.WebConfig;
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;
public class MyInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebConfig.class);
rootContext.setServletContext(servletContext);
rootContext.refresh();
DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
ServletRegistration.Dynamic dispatcherServlet1 = servletContext.addServlet("dispatcherServlet", dispatcherServlet);
dispatcherServlet1.addMapping("/");
}
}