Vaadin 与 Spring无引导和子上下文-“查找为空”

问题描述

我正在尝试实现以下架构:

  • 一个 spring 根上下文(包含业务逻辑)
  • 一个 vaadin 上下文
  • 一个 REST API 上下文 但该错误在单个项目中是可重现的,如下所示:

主包

// Entry Point

public final class Main {
  public static void main(String[] args) throws LifecycleException {
    Tomcat tomcat = new Tomcat();
    tomcat.setAddDefaultWebXmlToWebapp(false);
    tomcat.setPort(8080);
    tomcat.setHostname("localhost");
    tomcat.getConnector();

    Host host = tomcat.getHost();
    host.setAppBase(".");

    Context context = tomcat.addWebapp("",".");

    tomcat.start();
    tomcat.getServer().await();
  }
}

// Main Initializer
public class AppInitializer implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfig.class);

    servletContext.addListener(new ContextLoaderListener(rootContext));
  }
}

// Configuration - Would import DatabaseConfig etc.
@Configuration
@ComponentScan
public class RootConfig {
}

Vaadin 包

// Initializer

public class VaadinInitializer implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) {
    // This is more or less copied from VaadinMVCWebAppInitializer
    AnnotationConfigWebApplicationContext vaadinContext = new AnnotationConfigWebApplicationContext();
    vaadinContext.register(VaadinConfig.class);

    SpringServlet vaadinspringServlet = new SpringServlet(vaadinContext,false);
    ServletRegistration.Dynamic vaadinServletRegistration = servletContext.addServlet(
        ClassUtils.getShortNameAsProperty(SpringServlet.class),vaadinspringServlet);

    vaadinServletRegistration.addMapping("/app/*");
    vaadinServletRegistration.setAsyncSupported(true);
  }
}

// Config

@Configuration
@ComponentScan
@Import({VaadinScopesConfig.class,VaadinApplicationConfiguration.class})
public class VaadinConfig {
}

我收到以下异常:

org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [springServlet] in web application [] threw load() exception
java.lang.NullPointerException
    at com.vaadin.flow.server.DeploymentConfigurationFactory.getTokenFileFromClassloader(DeploymentConfigurationFactory.java:341)
    at com.vaadin.flow.server.DeploymentConfigurationFactory.getTokenFileContents(DeploymentConfigurationFactory.java:311)
    at com.vaadin.flow.server.DeploymentConfigurationFactory.readBuildInfo(DeploymentConfigurationFactory.java:181)
    at com.vaadin.flow.server.DeploymentConfigurationFactory.createInitParameters(DeploymentConfigurationFactory.java:174)
    at com.vaadin.flow.server.VaadinServlet.createDeploymentConfiguration(VaadinServlet.java:152)
    at com.vaadin.flow.server.VaadinServlet.createServletService(VaadinServlet.java:190)
    at com.vaadin.flow.server.VaadinServlet.init(VaadinServlet.java:77

Vaadin lookupnull,尽管它应该通过 VaadinApplicationConfiguration 设置。我想我在设置上下文层次结构/加载的方式上做错了。

版本:Spring 5.3.7、Vaadin 14.6.1、Vaadin-Spring:12.4.0(14.5.5 的错误略有不同,但根本原因可能相同)

为了完整性:Gradle 文件

plugins {
    id 'java'
    id 'com.vaadin' version '0.14.6.0'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.springframework',name: 'spring-context',version: "5.3.7"
    implementation group: 'org.springframework',name: 'spring-webmvc',version: "5.3.7"

    compileOnly group: 'javax.servlet',name: 'javax.servlet-api',version: "3.1.0"

    implementation group: 'com.vaadin',name: 'vaadin-core',version: "14.6.1"
    implementation group: 'com.vaadin',name: 'vaadin-spring',version: "12.4.0"

    implementation group: 'org.apache.tomcat.embed',name: 'tomcat-embed-core',version: "9.0.46"
    runtimeOnly group: 'org.apache.tomcat',name: 'tomcat-el-api',name: 'tomcat-jasper-el',name: 'tomcat-websocket',version: "9.0.46"
}

test {
    useJUnitPlatform()
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)