博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot踩坑出坑记
阅读量:4581 次
发布时间:2019-06-09

本文共 8210 字,大约阅读时间需要 27 分钟。

4月15到4月17我都在把毕设从eclipse重构到IDEA中,springboot最让我头疼的是它的版本问题,因为每一个版本对应的依赖包都有可能出错,这里分享一下如何成功移植用eclipse写的springboot到IDEA中,比较简单的步骤我这里不详细说了,说一下我遇到的一些很难找出问题的地方

ps:只是针对于我的项目和我个人水平,大神勿喷嘿嘿

springboot-mybatis整合坑

  • 出现下方错误请查看启动类:XXXApplication 是否扫描到mapper映射文件,声明eclipse和idea不一样,这里eclipse可以跑通,idea中不行
***************************        APPLICATION FAILED TO START        ***************************        Description:        Field chapterDao in cn.yixue.service.ChapterServiceImp required a bean of type 'cn.yixue.dao.ChapterMapper' that could not be found.        The injection point has the following annotations:            - @org.springframework.beans.factory.annotation.Autowired(required=true)        Action:        Consider defining a bean of type 'cn.yixue.dao.ChapterMapper' in your configuration.        以上提取出有用的信息:required a bean of type 'xxxx' that could not be found.                代表bean没注入,从bean注入寻找方向,有的人会说我用@Autowired之类的种种,但没扫到,好吧~

解决方法:

  1. 在相应的mapper类中加@Mapper标注让springboot根据标注去将mapper注入
@Mapperpublic interface ChapterMapper {    ......}
  1. 启动类加@MapperScan(value = "cn.yixue.video.dao") value 后的包一定要对应到mapper类对应的地方,比如我的mapper在dao下,就是cn.yixue.video.dao
@SpringBootApplication@MapperScan(value = "cn.yixue.video.dao")@EnableDiscoveryClientpublic class YixueVideoApplication {    public static void main(String[] args) {        SpringApplication.run(YixueVideoApplication.class, args);    }}
  1. Spring Boot项目中含有Mybatis,打Jar包运行之后,报如下错误:
***************************APPLICATION FAILED TO START***************************Description:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver classAction:Consider the following:    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

网上好多解决方案,针对于每个人都不一样,我的应该是打包的时候读不到我的配置文件,需要在pom.xml里面加resourses指定下配置文件,因为eclipse是识别的,Idea可能不会?我也不太知道,反正是加上了,因为好像有Idea读不到我的application.properties或者application.yml文件,我就一次性都配上了,这个大家具体遇到的时候再去搜一下就行,不用刻意的记:

src/main/java
**/*.yml
**/*.properties
**/*.xml
false
src/main/resources
**/*.yml
**/*.properties
**/*.xml
false

springBoot-SpringCloud整合坑

  • 利用SpringCloud做服务注册时,Eclipse需要自己导jar包依赖和配置版本,Idea直接可以再创建Springboot项目时鼠标点击引入,这个我就放几张图来解释:

1335038-20190604101911337-1634357883.png

1335038-20190604102035889-709190192.png
1335038-20190604102041205-1925143521.png

最后一个next后直接finish......

之后再pom.xml里面会看到Idea自动为你引入的依赖和 spring-boot-maven-plugin 插件,插件版本我建议还是稍微低一点,因为boot真的是随着版本变动改动很大,我用的是

org.springframework.boot
spring-boot-maven-plugin
1.5.9.RELEASE

这个也是在网上搜了很久之后找到的一个版本,还有一个1.4.9.RELEASE也可以,之后就是看看Idea导入的SpringCloud依赖的版本version,版本错误很容易报java.lang.AbstractMethodError: null这个错误我找了很久,原因也是看了一个找到的,具体就是因为Idea给你的依赖是根据你选择的springboot的版本来的,一般人不会去修改,这也就是为什么eclipse不容易报错,Idea容易的原因,因为eclipse得自己找...

org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE

我的parent的版本是<version>2.0.5.RELEASE</version>,大佬的文章也提到了2.1.0和2.1.0以下的有区别,我还是建议用低的,低的差别不会太大,还挺稳......我还用过1.5.9.RELEASE...

  • 之后配置eureka的服务的时候Idea提供的版本也要改,这个原因是因为如果使用${spring-cloud.version}的话,当版本号下调到2.1.0以下的时候,一些组件的包还是2.1.0它不会跟随parent版本的下调而下调,也就是parent的版本小于组件的版本,这时候就会出问题
    当改为Finchley.RELEASE的时候,组件的依赖就会跟随parent的版本下调而下调
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import

解决静态资源跨域时的坑

  • 之前在eclipse中静态资源访问是可以通过
@Autowiredprivate RestTemplate restTemplate;

直接装配的,之后到了Idea中报错了:

ERROR 31473 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************APPLICATION FAILED TO START***************************Description:Field restTemplate in 'xxxxxx'required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.Action:Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

解决方法如下,大致就是先靠@Bean装配,再用...:

@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder) {   // Do any additional configuration here   return builder.build();}@Autowiredprivate RestTemplate restTemplate;

之后就不报错了(针对于我的错误)

我的 pom.xml(project的xml)

我的架构

1335038-20190604102005939-594360133.png

圈住的地方是下方的pom.xml文件

4.0.0
cn.yixue
yixue
1.0-SNAPSHOT
pom
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
1.8
1.14.8
1.2.31
org.springframework.boot
spring-boot-starter
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
5.1.46
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
true
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
${lombok.version}
provided
com.alibaba
fastjson
${fastjson.version}
org.springframework.boot
spring-boot-maven-plugin
1.5.9.RELEASE
true
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
yixue-commom
yixue-admin
yixue-video

这就是我移植项目遇到的一些问题,下面列一些大佬的博客,对我帮助很大,不胜感激

如有侵权,请联系我删除

转载于:https://www.cnblogs.com/cxylff/p/10969375.html

你可能感兴趣的文章
PoolThreadCache
查看>>
使grub4dos引导启动linux
查看>>
P2597 [ZJOI2012]灾难
查看>>
LeetCode--LinkedList--206. Reverse Linked List(Easy)
查看>>
最短路径
查看>>
Console-算法[for,if]-一水仙花数(Water flower)
查看>>
IntelliJ-IDEA和Git、GitHub、Gitlab的使用
查看>>
Request——Node世界中被依赖最多的库No.2
查看>>
【共读Primer】26.[4.6]成员访问运算符 Page133
查看>>
8、OpenCV Python 图像直方图
查看>>
判断远程主机是否存在某个文件
查看>>
[SDOI2011]工作安排
查看>>
block change tracking buffer space
查看>>
简单API练手:(1)复制自身程序到windows目录和系统目录下;(2)获得系统的相关信息。...
查看>>
codeforces 722D Generating Sets 【优先队列】
查看>>
【并发编程】延时初始化
查看>>
编程珠玑--左旋字符串
查看>>
【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十四:储存模块
查看>>
模板 - 字符串 - Manacher
查看>>
2017.1.2
查看>>