不用 spring boot 和xml文件,只使用 spring 注解实现最简单的 spring mvc 内嵌 tomcat 启动
主要用到的注解
@ComponentScan 扫描类添加到AnnotationConfigWebApplicationContext中
其他注解
@Service、@Autowired、@RestController、@GetMapping
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gcy.demo</groupId>
<artifactId>java-thread-demo</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.32</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
MyService.java
package com.gcy.demo.javathreaddemo.myservlet;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String getTest(){
return "这是最简单的web项目测试";
}
}
MyController
package com.gcy.demo.javathreaddemo.myservlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private MyService myService;
//设置 produces 防止中文乱码
@GetMapping(value = "/test",produces = "application/json; charset=utf-8")
public String test(){
String str = myService.getTest();
System.out.println("------MyController "+str+"----------");
return str;
}
}
MyWebApplication.java 最主要启动类
package com.gcy.demo.javathreaddemo.myservlet;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import java.io.File;
@ComponentScan
public class MyWebApplication {
public static void main(String[] args) throws LifecycleException {
File base = new File(System.getProperty("java.io.tmpdir"));
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.addContext("/",base.getAbsolutePath());
tomcat.start();
//容器添加需要启动的ApplicationContext
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext
= new AnnotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.register(MyWebApplication.class);
annotationConfigWebApplicationContext.refresh();
//创建 servlet 并添加到 tomcat 中
DispatcherServlet dispatcherServlet = new DispatcherServlet(annotationConfigWebApplicationContext);
Wrapper wrapper = tomcat.addServlet("/","app",dispatcherServlet);
wrapper.setLoadOnStartup(1);
wrapper.addMapping("/");
tomcat.getServer().await();
}
}
请求地址:http://localhost:8080/test
请求成功