spring boot开发遇到的一些坑

本文是个人在做spring boot微服务改造时遇到的一些坑,简单记录一下。

一、spring boot热部署

spring boot热部署有两种方式:

在spring-boot-maven-plugin中添加springloaded依赖,pom如下:

<plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin </artifactId>
          <dependencies>  
          <dependency>  
              <groupId>org.springframework</groupId>  
              <artifactId>springloaded</artifactId>  
              <version>1.2.4.RELEASE</version>
          </dependency>  
       </dependencies>  
       <executions>  
          <execution>  
              <goals>  
                  <goal>repackage</goal>  
              </goals>  
              <configuration>  
                  <classifier>exec</classifier>  
              </configuration>  
          </execution>  
      </executions>
</plugin>


坑:添加依赖后热部署不起作用

解决方案:

1、设置IDEA允许应用自动编译,共两处设置

2、spring boot应用必须使用spring boot-run的方式启动

3、spring boot-run启动时,maven的仓库地址中不能有空格,否则会导致启动报错,找不到springloaded的jar包。


使用dev_tools:

添加jar包依赖,spring-boot-maven-plugin中添加fork参数,该种方式对热部署的支持更全面一些,但修改后会导致应用频繁重启,个人最后使用的是该种方式


二、spring boot添加mybatis支持


1、使用自定义mybatis配置文件

在spring boot配置中添加:

mybatis:
  mapper-locations: classpath:mapper/*.xml
  config-location: classpath:mybatisConfiguration.xml


坑:使用mybatis时自定义的分页插件时,报错:找不到方法StatementHandler.prepare

解决方案:mybatis 3.4 版本StatementHandler.prepare添加了一个int类型参数,修改如下:

@Intercepts({@Signature(type =StatementHandler.class, method = "prepare", args ={Connection.class, Integer.class})


坑:spring boot添加mybatis后启动时报错:required a single bean, but 2 were found

解决方案:在spring boot启动类的@MapperScan注解中指定annotationClass:

@MapperScan(value = "com.asiainfo.microservice", annotationClass = org.springframework.stereotype.Repository.class)

当然,对应dao接口需要添加@Repository注解


三、spring boot服务依赖


坑:找不到依赖jar包中的类

解决方案:spring boot使用spring-boot-maven-plugin打出的jar包不能直接提供给第三方依赖,需修改配置单独打一个可依赖的jar包,如A依赖B,

在B中添加:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <executions>
        <execution>
          <id>api-jar</id>
          <phase>package</phase>
          <goals>
            <goal>jar</goal>
          </goals>
          <configuration>
            <classifier>api</classifier>
          </configuration>
        </execution>
      </executions>
    </plugin>


在A中添加:

<dependency>
      <groupId>XXXX</groupId>
      <artifactId>XXXX</artifactId>
      <version>1.0-SNAPSHOT</version>
      <classifier>api</classifier>
    </dependency>



四、spring boot自动配置


坑:找不到配置

描述:如A依赖的第三方jar包B中包含mybatis,在A启动时spring boot就会自动查找datasource配置,找不到就报错

解决方案:注意依赖传递,禁用无关依赖



五、spring boot扫描范围

在spring boot 的启动类可以通过@ComponentScan注解指定扫描范围,不加的话默认扫描启动类所在包及其子包,

如依赖的第三方jar中有需要扫描的类,需在@ComponentScan中指定。


6、restTemplate服务调用

使用restTemplate进行微服务之间的调用,get方法拼接url参数时,如果有json格式的参数,会报错url不合法,

url合法字符中不包含{、}、“,无法直接用get方法传递json参数。如对特殊字符转义的话,会导致jackson解析json

为object出错,该问题未解决,目前使用方式为不在get方法中传递json格式参数。







猜你喜欢

转载自blog.csdn.net/qq_43036122/article/details/85855570