繁体   English   中英

春季引导。 该类仅在src / java / main中运行,在src / java / test中具有异常

[英]Spring boot. Class run only in src/java/main, in src/java/test it has exceptions

  1. 我有一个java / spring-boot类
@SpringBootApplication
@ComponentScan(value = "ignore")
@RunWith(SpringRunner.class)
public class MyApplication {
   @Bean
   YAMLConfig myeadFromYMLFile() {
      return new YAMLConfig();
   }
   public static void main(String[] args) throws InterruptedException {
      ConfigurableApplicationContext context = 
         SpringApplication.run(MyApplication.class, args);
      YAMLConfig myBean = context.getBean(YAMLConfig.class);
      myBean.doSomething();
      myBean.printApi();
   }
}
  1. 并具有配置文件,该文件从yml文件读取数据
@Configuration
@EnableConfigurationProperties
public class YAMLConfig {
   @Value("${refresh.rate}")
   private int refreshRate;
   @Value("${datasource.apiUrl}")
   private String apiUrl;
   public void doSomething() {
      System.out.printf("Refresh Rate : %s%n", refreshRate);
   }
   public void printApi() {
      System.out.printf("Refresh Rate : %s%n", apiUrl);
   }
   public String getApiUrl() {
      return apiUrl;
   }
}

问题:当我在src / java / main中运行类MyApplication时,一切都很棒。 但是:当我在src / java / test中运行此类时,它将失败

启动ApplicationContext时出错。 要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2018-09-06 17:14:21.389错误16515 --- [main] osboot.SpringApplication:应用程序运行失败org.springframework.context.ApplicationContextException:无法启动Web服务器;无法启动Web服务器。 嵌套的异常是java.lang.NoClassDefFoundError:org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)上的javax / servlet / http / HttpSessionIdListener(〜spring-boot-2.0.4.RELEASE .jar:2.0.4.RELEASE]在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)〜[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]在org .springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.SpringApplication处。在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)的refresh(SpringApplication.java:762)[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] [spring-boot-2.0 .4.RELEASE.jar:2.0.4.RELEASE]位于org.springframework.boot.SpringApplication.run(SpringApplication.java:330)[spring-boot-2.0.4.RELEASE.jar:2.0.4。 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)中发布[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.SpringApplication.run(SpringApplication中) .java:1246)位于MyApplication.main(MyApplication.java:19)的[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] [test-classes /:na]造成原因:java.lang。 NoClassDefFoundError:io.undertow.servlet.core.ApplicationListeners。(ApplicationListeners.java:62)〜[undertow-servlet-1.4.25.Final.jar:1.4.25.Final]位于io.io.undertow.servlet.core。 undertow.servlet.spec.ServletContextImpl.ensureNotProgramaticListener(ServletContextImpl.java:875)〜[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.spec.ServletContextImpl.getSessionCookieConfig(ServletContextImpl .java:648)〜[undertow-servlet-1.4.25.Final.jar:1.4.25.Final] at io.undertow.servlet.core.DeploymentManagerImpl.handleDeploymentSessionConfig(DeploymentManagerImpl.java:622)〜[undertow-servlet- 1.4.25.Final.jar:1.4.25.Fina l]在io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:167)〜[undertow-servlet-1.4.25.Final.jar:1.4.25.Final]在org.springframework.boot.web处。 org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory.getWebServer的Embedded.undertow.UndertowServletWebServerFactory.createDeploymentManager(UndertowServletWebServerFactory.java:284)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] (UndertowServletWebServerFactory.java:208)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) 〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)〜[spring-boot-2.0 .4.RELEASE.jar:2.0.4.RELEASE] ...省略了8个共同的框架,原因是:java.lang.ClassNotFoundException:javax.servlet.http.HttpSession 位于java.net.URLClassLoader.findClass(URLClassLoader.java:381)处的IdListener(位于java.lang.ClassLoader.loadClass(ClassLoader.java:424)〜[na:1.8.0_181]处的[na:1.8.0_181]〜 misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)〜[na:1.8.0_181]在java.lang.ClassLoader.loadClass(ClassLoader.java:357)〜[na:1.8.0_181] ... 17常见框架省略

PSI具有spring-boot-starter-web依赖性。 通过@SpringBootTest我得到了:

org.springframework.context.ApplicationContextException:无法启动Web服务器;无法启动Web服务器。 嵌套的异常是org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext。 在org.springframework.context上的org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]。 support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)〜[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext .java:140)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)[spring-boot-2.0.4 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)的[RELEASE.jar:2.0.4.RELEASE](org.org的[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]]。 springframework.boot.SpringApplication.run(SpringApplication.java:330)[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]在org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [SPRI org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)的ng-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] [spring-boot-2.0.4.RELEASE.jar:2.0。 4.RELEASE]在MyApplication.main(MyApplication.java:19)[测试类/:na]原因:org.springframework.context.ApplicationContextException:由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext。 在org.springframework.boot的org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]中。 web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]位于org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext .onRefresh(ServletWebServerApplicationContext.java:152)〜[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE] ...省略了8个常见框架

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM