2017-02-15 99 views
0

我正嘗試在獨立tomcat中運行簡單的spring啓動應用程序。我試過在這裏參考文檔和一些問題/答案,但似乎沒有任何工作。當tomcat啓動時,它會部署應用程序,但完全忽略我的Application類,因此不會加載彈簧代碼。我相信它一定是簡單的我想念但是...?春季啓動應用程序無法在獨立tomcat中加載

這裏的日誌文件中的相關輸出:

INFO: Deploying web application archive C:\dev\servers\apache-tomcat-7.0.75\webapps\bankweb-0.1.war 
Feb 15, 2017 4:41:45 PM org.apache.catalina.startup.TldConfig execute 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list o 
f JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and 
JSP compilation time. 
Feb 15, 2017 4:41:45 PM org.apache.catalina.startup.HostConfig deployWAR 
INFO: Deployment of web application archive C:\dev\servers\apache-tomcat-7.0.75\webapps\bankweb-0.1.war has finished in 3,595 
ms 

我已經更新了應用類SpringBootServletInitializer延長按照文檔:

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     SpringApplication.run(applicationClass, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(applicationClass); 
    } 

    private static Class<Application> applicationClass = Application.class; 
} 

@RestController 
class GreetingController { 

    @RequestMapping("/hello/{name}") 
    String hello(@PathVariable String name) { 
     return "Hello, " + name + "!"; 
    } 
} 

而在POM我已經改變打包打仗並標記爲嵌入式servlet容器的提供:

<?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>it.ankle</groupId> 
    <artifactId>bankweb</artifactId> 
    <version>0.1</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.5.1.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.7</java.version> 
     <tomcat.version>7.0.69</tomcat.version> 
    </properties> 

    <dependencies> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jersey</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <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>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.projectlombok</groupId> 
      <artifactId>lombok</artifactId> 
      <optional>true</optional> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

    </dependencies> 

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

我在Application類中看到的只有重寫的'SpringApplicationBuilder configure()'方法,我沒有這個方法。作爲附註,你可以用'@ SpringBootApplication'替換'@Configuration @EnableAutoConfiguration @ ComponentScan' – Gregg

回答

0

這已解決,用戶錯誤幾乎總結了它。我剝去了所有的可選彈簧模塊,讓它工作,然後將所有東西一點一點地加進去,並且工作。我不是很習慣這個春季啓動的東西,但功能被包括在內,只是因爲你在POM中配置了一個庫。感謝任何花時間閱讀的人!

相關問題