1、前言碎语

博主公司一个项目在开发中使用某些功能的时候,受限于spring低版本的限制,故索性将整个模块升级为spring boot,在这里做个记录,希望能帮助到有相同场景的朋友。

整个改造过程非常简单,耗时大概在2个小时左右,主要解决项目中的各种版本冲突,不过下面我会介绍一个神器。

2、老项目情况

1.项目使用spring-context作为容器,使用RabbitMQ提供RPC服务

2.spring.springframework 版本比较低,3.1.x的版本,升级后会变成4.3.x

3.项目使用maven构建

以上是项目的基本情况,针对如上情况,下面会详细描述改造过程中需要的关注点。

第一步:添加spring boot依赖

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.7.RELEASE</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
</dependencies>

第二步:新增spring boot启动类,加载原先的xml配置

/**
* Created by kl on 2018/1/29.
* Content :Lbt-service-ext服务启动器
*/@SpringBootApplication(exclude = {RabbitAutoConfiguration.class})@ImportResource("service-context.xml")publicclassLbtServiceExtApplication{
publicstaticvoid main(String[] args) {
  SpringApplication application= newSpringApplication(LbtServiceExtApplication.class);
  application.setWebEnvironment(false);
  application.run(args);
}}

注意的地方:

1.排除了RabbitMQ的自动装载了,因为在xml中已经配置过了RabbitMQ的相关连接和服务信息了

2.设置了setWebEnvironment(false),标记项目为非web项目,因为只是提供RPC服务,所以不需要servlet容器。

第三步:尝试启动,排除jar冲突

这个时候可以启动main方法,看看能否启动了,一般情况下没那么容易就能启动起来,会有各种的jar冲突。我们项目从3.x到4.x,更是各种冲突。

下面介绍一个插件,破除jar冲突排除的烦恼,前提是在IDEA下开发,eclipse应该也有类似的。

插件名字:Maven Helper

可以代替mvn dependency:tree命令的使用了,这个插件可以更直观的列出项目依赖的jar,非常牛逼的是可以直接列出项目中有冲突的jar,这对找jar冲突非常有用,而且可以直接右键排除掉。

jar相关异常识别技巧:

出现NoSuchMethodError:一般都是jar冲突了

出现ClassNotFoundException:缺少相关的jar了

三步做完后,项目妥妥的跑起来了。

3、Spring Boot怎么识别web项目

1.spring boot会识别项目是否是web项目,如果识别到事web项目,又没有添加tomcat等容器jar,就抛异常。

2.识别的方式就是看项目是否依赖了servlet-api和spring-web。而我们项目需要spring-web相关如el等功能又不需要tomcat容器,所以可以指定为非web项目。

3.排除掉tomcat后,项目jar体积和运行时内存占用都有很大的改善。

来源:http://www.kailing.pub/article/index/arcid/188.html

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注