2016-11-30 65 views
0

這是整個的pom.xmlspringboot應用程序如何在main方法中運行並在tomcat中運行,其配置與下面相同?

<?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.example</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
     <start-class>com.example.Application</start-class> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency>    

    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


</project> 

的應用類應該是這樣的

Application.java

package com.example; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.stereotype.Controller; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 

@SpringBootApplication 
@Controller 
public class Application extends SpringBootServletInitializer { 


    /** 
    * Used when run as JAR 
    */ 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @RequestMapping("/") 
    public @ResponseBody String hello(){ 
     return "hello world!"; 
    } 

    /** 
    * Used when run as WAR 
    */ 
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.sources(Application.class); 
    } 

} 

整個項目是你可以爲above.However看到,主要方法不能運行,如果我設置範圍

 <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 

提供。但是,如果我刪除範圍,主要方法可以正常運行。問題是我應該怎麼做而不刪除範圍運行主要方法。

+0

您需要先學習的'@ controller'註釋 – emotionlessbananas

+0

spring-boot-maven-plugin將提供的'作用域依賴包打包成'WEB-INF/lib提供的',該位置不會被容器挑選當您將它部署爲.WAR時。您應該將異常堆棧跟蹤發佈到__「主要方法無法運行」__供其他人幫忙。 – tan9

回答

0
  • 在maven中,當您省略scope時,它默認爲compile
  • 當您在servlet容器外面運行時(即作爲java 應用程序),您需要提供的compile(或刪除範圍,因爲compile是默認值)。這樣做是爲了將這些罐子添加爲依賴關係。
  • 在servlet容器(tomcat或jetty)中運行時,scope可能設置爲provided,因爲這些jar由servlet容器在運行時提供,不應包含在打包的應用程序中。

現在,爲了解決手頭上的問題,您可以使用maven配置文件。執行maven任務時,您可以使用-P開關激活配置文件。

+0

非常感謝!它爲我提供了一個好主意! – Wulh

+0

@Wulh請接受答案,如果它幫助你! –

相關問題