War Deploy

War 包部署

pom packaging

<packaging>war</packaging>

继承 SpringBootServletInitializer

/**
 * 需要继承 SpringBootServletInitializer
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        Application.run(LifecycleApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}

原理

  • javax.servlet.ServletContainerInitializer
    • spring-web : org.springframework.web.SpringServletContainerInitializer

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {

	@Override
	public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
		...
  
		AnnotationAwareOrderComparator.sort(initializers);
		for (WebApplicationInitializer initializer : initializers) {
			initializer.onStartup(servletContext);
		}
	}

}

  • spring-web : org.springframework.web.WebApplicationInitializer
    • spring-boot : SpringBootServletInitializer
    • spring-web : AbstractContextLoaderInitializer
      • spring-webmvc : AbstractDispatcherServletInitializer
        • spring-webmvc : AbstractAnnotationConfigDispatcherServletInitializer

Read More