欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot Tomcat启动实例代码详解

程序员文章站 2024-02-28 18:09:58
废话不多了,具体内容如下所示: application configuration class: @springbootapplication public...

废话不多了,具体内容如下所示:

application configuration class:
@springbootapplication
public class servletinitializer extends springbootservletinitializer {
  @override
  protected springapplicationbuilder configure(springapplicationbuilder application) {
    return application.sources(servletinitializer.class);
  }
  public static void main(string[] args) throws exception {
    springapplication.run(servletinitializer.class, args);
  }
}

注意: 启动类放在项目的包的最外层最好,这样可以扫描到所有的包路径。

controller:

@controller
public class bootcontroller {
  @requestmapping("/")
  @responsebody
  string home() {
    return "hello world!";
  }
  public static void main(string[] args) throws exception {
    springapplication.run(bootcontroller.class, args);
  }
}

pom

<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>cn.creditease.springboot</groupid>
  <artifactid>springboot</artifactid>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>maven webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project_charset>utf-8</project_charset>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <tomcat.version>7.0.67</tomcat.version>
  </properties>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.4.1.release</version>
  </parent>
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-tomcat</artifactid>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <repositories>
      <repository>
        <id>spring-releases</id>
        <name>spring releases</name>
        <url>http://repo.spring.io/libs-release-local</url>
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
      </repository>
  </repositories>
</project>

注意:如果想用tomcat7启动要制定你的tomcat版本号。

server:
 port: 8080
 spring.mvc.view.prefix: /web-inf/jsp/
 spring.mvc.view.suffix: .jsp

项目

SpringBoot Tomcat启动实例代码详解

总结

以上所述是小编给大家介绍的springboot tomcat启动实例代码详解,希望对大家有所帮助