2017-10-04 177 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