2017-10-04 139 views
1

我試圖將一個最小的Vaadin/SpringBoot應用程序作爲WAR文件部署到獨立的Tomcat中。如果我跑gradle vaadinRun和訪問localhost:8080下,但創建與gradle war WAR文件,然後複製它變成我的Tomcat結果的webapps文件夾中404不幸的是Tomcat的日誌不顯示任何東西作爲WAR部署Vaadin/SpringBoot

一切正常。嘗試通過localhost:8080/hello-vaadin訪問。

這裏是應用類本身:

package com.somecompany; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.Bean; 
import org.springframework.boot.autoconfigure.*; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.boot.web.servlet.ServletRegistrationBean; 
import org.springframework.boot.web.servlet.ServletComponentScan; 
import com.vaadin.spring.annotation.EnableVaadin; 

@ServletComponentScan 
@SpringBootApplication 
@EnableVaadin 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) throws Exception { 
     configureApplication(new SpringApplicationBuilder()).run(args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return configureApplication(builder); 
    } 

    private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) { 
     return builder.sources(Application.class); 
    } 

} 

這是對應的UI類:

package com.somecompany; 

import com.vaadin.annotations.Theme; 
import com.vaadin.spring.annotation.SpringUI; 
import com.vaadin.server.VaadinRequest; 
import com.vaadin.ui.UI; 
import com.vaadin.ui.Grid; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.vaadin.ui.Label; 

@SpringUI 
@Theme("valo") 
public class HelloWorldUI extends UI { 

    @Autowired 
    public HelloWorldUI() { 

    } 

    @Override 
    protected void init(VaadinRequest request) { 
     setContent(new Label("Hello World!")); 
    } 

} 

最後我gradle這個腳本:

plugins { 
    id "java" 
    id "com.devsoap.plugin.vaadin" version "1.2.4" 
    id "org.springframework.boot" version "1.5.7.RELEASE" 
} 

jar { 
    baseName = 'com.somecompany.hello-vaadin' 
    version = '0.0.1-SNAPSHOT' 
} 

apply plugin: 'war' 

war { 
    baseName = 'hello-vaadin' 
    version = '1.0' 
} 

springBoot { 
    mainClass = 'com.somecompany.Application' 
} 

bootRepackage { 
    mainClass = 'com.somecompany.Application' 
} 

repositories { 
    jcenter() 
    mavenCentral() 
    maven { url "http://oss.sonatype.org/content/repositories/vaadin-snapshots/" } 
    maven { url 'https://repo.spring.io/libs-release' } 
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" } 
} 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-web") 
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") 
} 

通過教程去後在教程之後,必須有一些我忽略的東西。但是,我不明白這個問題會是什麼?

任何提示高度讚賞!

+0

作爲maven用戶,我需要spring-boot-maven-plugin,也許還有一個等價的gradle呢? – mrkernelpanic

+0

@Jay是的,我可以從經理頁面看到應用程序。 – Tobias

+0

@mrkernelpanic這應該在第四行org.springframework.boot插件中完成。 – Tobias

回答

2

最後,我設法使它工作。這就是:

MyApplication.java

package com.somecompany; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class MyApplication { 
    public static void main(String[] args) throws Exception { 
     SpringApplication.run(MyApplication.class, args); 
    } 
} 

MyServletInitializer.java

package com.somecompany; 

import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.boot.builder.SpringApplicationBuilder; 

public class MyServletInitializer extends SpringBootServletInitializer { 
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.sources(MyApplication.class); 
    } 
} 

MyConfiguration.java

package com.somecompany; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Bean; 

@Configuration 
public class MyConfiguration { 
    @Bean 
    public String myLabelString() { 
     return "Hello World Bean!"; 
    } 
} 

HelloWorldUI.java

package com.somecompany; 

import com.vaadin.spring.annotation.SpringUI; 
import com.vaadin.server.VaadinRequest; 
import com.vaadin.ui.UI; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.vaadin.ui.Label; 

@SpringUI 
public class HelloWorldUI extends UI { 

    @Autowired 
    String helloWorldString; 

    @Override 
    protected void init(VaadinRequest request) { 
     if (helloWorldString != null) { 
      setContent(new Label(helloWorldString)); 
     } else { 
      setContent(new Label("Injection does not work!")); 
     } 
    } 
} 

的build.gradle

plugins { 
    id 'com.devsoap.plugin.vaadin' version '1.2.1' 
    id 'org.springframework.boot' version '1.5.3.RELEASE' 
} 

apply plugin: 'war' 

war { 
    baseName = 'hellovaadin' 
} 

springBoot { 
    mainClass = 'com.somecompany.MyApplication' 
} 

然後用gradle build我構建WAR文件,然後將其複製到我的Tomcat實例的webapps文件夾。

我已經擴展了該示例,同時還顯示瞭如何注入/自動裝入一個bean。

+0

你甚至可以使用構造函數注入iso字段注入來改進你的代碼。 – Thibstars