美文网首页
springboot启动非web工程及遇到的问题

springboot启动非web工程及遇到的问题

作者: 夜亦无影 | 来源:发表于2019-11-14 10:57 被阅读0次

    最近领导要求将老的java工程转为springboot架构启动,由于第一次做,艰辛过程中自己探索,最后终于启动成功。

    1.首先将java工程转为maven工程,此步骤(比较繁琐,需要对大量jar包做依赖)网上很多,此处省略。。。

    2.完成maven工程构建后,由于我们的项目不需要提供 Web 服务,如果按照 Web 项目启动未免画蛇添足浪费资源为了达到非 Web 运行的效果,首先调整 Maven 依赖,不再依赖 spring-boot-starter-web,转而依赖最基础的 spring-boot-starter:

    pom.xml中:

    此时按照原先的方式启动 SpringBootApplication 会发现启动加载完之后会立即退出,这时需要利用 SpringBoot 提供的 CommandLineRunner 接口特性让主线程阻塞让程序不退出,这个名字比较有欺骗性,实际效果如下:

    SpringBoot 应用程序在启动后,会遍历 CommandLineRunner 接口的实例并运行它们的 run 方法。也可以利用 @Order 注解(或者实现Order接口)来规定所有 CommandLineRunner 实例的运行顺序 

    springboot启动类:

    service服务:

    启动成功:

    期间遇到的问题与大家分享:

    1.gson依赖版本过低

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.gson.GsonBuilder]: Factory method 'gsonBuilder' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: 

    原版本:

    改为:

    2.ComponentScan未注解:

    Description: A component required a bean of type 'com.berchina.mina.main.ServiceSearcher' that could not be found. Action: Consider defining a bean of type 'com.berchina.mina.main.ServiceSearcher' in your configuration.

    添加相应的注解扫描包:

    希望以上对有问题的同学有所帮助~

    相关文章

      网友评论

          本文标题:springboot启动非web工程及遇到的问题

          本文链接:https://www.haomeiwen.com/subject/thglictx.html