pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
启动springmvc的配置
@Configuration
@ComponentScan("com.demo")
public class AppConfig {
}
/**
* @author zc
* @version 1.0
* @date 2020/1/12 1:04 下午
* @desc
*/
public class MyWebAppInit implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("+++++++++++++++++++++");
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
registration.addMapping("/");
registration.setLoadOnStartup(1);
}
}
启动tomcat
/**
* @author zc
* @version 1.0
* @date 2020/1/12 2:42 下午
* @desc
*/
public class MySpringApplication {
public static void run() throws ServletException, LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(9090);
//告訴tomcat你的源码在哪里
String sourcePath = MySpringApplication.class.getResource("/").getPath();
Context context = tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());
WebResourceRoot resources = new StandardRoot(context);
resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
sourcePath, "/"));
context.setResources(resources);
tomcat.start();
tomcat.getServer().await();
}
}
controller
@Controller
public class UserController {
@RequestMapping("/test")
@ResponseBody
public String test() {
System.out.println("***************");
return "hello spring mvc";
}
}
启动类
public class MySpringStart {
public static void main(String[] args) throws ServletException, LifecycleException {
MySpringApplication.run();
}
}
项目顺利启动
访问一下
真正的启动方式
上面的方式并不是springboot真正的启动方式,他是不依赖ServletContainerInitializer的
注意下面这行代码,addWebApp之后,tomcat就会认为你要启动的是一个web项目,此时tomcat才回去执行实现ServletContainerInitializer的类
Context context = tomcat.addWebapp("/", new File("src/main/webapp").getAbsolutePath());
如果没有这行代码,就无法解析
我们来修改一下run方法
public class MySpringApplication {
public static void run() throws ServletException, LifecycleException {
File file = new File(System.getProperty("java.io.tmpdir"));
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
Tomcat tomcat = new Tomcat();
tomcat.setPort(9090);
//告訴tomcat你的源码在哪里
String sourcePath = MySpringApplication.class.getResource("/").getPath();
// Context context = tomcat.addWebapp("/", file.getAbsolutePath());
Context context = tomcat.addContext("/", file.getAbsolutePath());
DispatcherServlet dispatcherServlet = new DispatcherServlet(ac);
Tomcat.addServlet(context, "testServlet", dispatcherServlet);
context.addServletMapping("/", "testServlet");
tomcat.start();
tomcat.getServer().await();
}
}
注意下面的代码
Context context = tomcat.addContext("/", file.getAbsolutePath());
这种情况下tomcat就不会去解析ServletContainerInitializer的实现类
所以我们就要在这个方法中去初始化spring环境,然后把servlet交给tomcat.
所以最后只需要这个run方法和AppConfig.java即可