如何在Spring Framework MVC应用程序中嵌入Tomcat? [英] How do I embed Tomcat in a Spring Framework MVC application?
问题描述
我已经创建了所需的配置/控制器类。但是我不清楚我应该如何编排这些类来运行tomcat实例。我知道使用Spring启动它是一个使用SpringApplication.run(..)的问题。但我正在尝试探索Spring Boot之前使用的替代方法。我对Spring Framework有点新意,请原谅我的无知。我也没有使用任何只使用Java的XML配置
I have created the required configurations/controller classes. But it's not clear to me how I should orchestrate these classes to use run a tomcat instance. I know with spring boot it's a matter of using SpringApplication.run(..). But I'm trying to explore the alternate method used prior to Spring Boot. I'm a bit new to the Spring Framework so forgive my ignorance. I'm also not using any XML configuration only using Java
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
@Override //....
protected String[] getServletMappings(){
return new String[] { "/" };
}
@Override //...
protected Class<?>[] getRootConfigClasses(){
return new Class<?>[] { RootConfig.class };
}
@Override //.....
protected Class<?>[] getServletConfigClasses(){
return new Class<?>[] { WebConfig.class };
}
}
我创建了一个控制器
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(method = RequestMethod.GET)
public String home(){
return "home";
}
POM文件:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
推荐答案
最后解决了我遇到的问题。我就像VitalyZ推荐的那样,在我的POM中添加了一个 Tomcat 的嵌入式实例。我在一个新类中配置了嵌入式tomcat实例。
Finally fixed the problem I was coming across. I added an embedded instance of Tomcat to my POM just like VitalyZ recommended. I configured the embedded tomcat instance in a new class.
在我的Pom文件中添加以下内容
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.15</version>
</dependency>
创建一个名为Application.java的新类
public class Application {
public static void main(String[] args) throws Exception {
String webAppDirLocation = "src/main/";
Tomcat tomcat = new Tomcat();
//Set Port #
tomcat.setPort(8080);
StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webAppDirLocation).getAbsolutePath());
tomcat.start();
tomcat.getServer().await();
}
}
这篇关于如何在Spring Framework MVC应用程序中嵌入Tomcat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!