springboot踩坑出坑记

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注入
@Mapper
public interface ChapterMapper {
    ......
}
  1. 启动类加@MapperScan(value = "cn.yixue.video.dao") value 后的包一定要对应到mapper类对应的地方,比如我的mapper在dao下,就是cn.yixue.video.dao
@SpringBootApplication
@MapperScan(value = "cn.yixue.video.dao")
@EnableDiscoveryClient
public 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 class

Action:

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文件,我就一次性都配上了,这个大家具体遇到的时候再去搜一下就行,不用刻意的记:

<build>
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
</build>

springBoot-SpringCloud整合坑

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



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

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

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.9.RELEASE</version>
</plugin>

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

我的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的版本下调而下调
<properties>
    <java.version>1.8</java.version>
    <!-- 这是Idea给设的 -->
    <!--<spring-cloud.version>Greenwich.SR1</spring-cloud.version>-->
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

解决静态资源跨域时的坑

  • 之前在eclipse中静态资源访问是可以通过
@Autowired
private 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装配,再用...:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
   // Do any additional configuration here
   return builder.build();
}
@Autowired
private RestTemplate restTemplate;

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

我的 pom.xml(project的xml)

我的架构


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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.yixue</groupId>
    <artifactId>yixue</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <lombok.version>1.14.8</lombok.version>
        <fastjson.version>1.2.31</fastjson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.9.RELEASE</version>
                <configuration>
                    <!--该配置必须-->
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <modules>
        <module>yixue-commom</module>
        <module>yixue-admin</module>
        <module>yixue-video</module>
    </modules>
</project>

这就是我移植项目遇到的一些问题,下面列一些大佬的博客,对我帮助很大,不胜感激
如有侵权,请联系我删除

原文地址:https://www.cnblogs.com/cxylff/p/10969375.html

时间: 2024-08-29 21:36:58

springboot踩坑出坑记的相关文章

SpringBoot踩坑日记-定时任务不定时了?

问题描述br/>springboot定时任务用起来大家应该都会用,加两注解,加点配置就可以运行.但是如果仅仅处在应用层面的话,有很多内在的问题开发中可能难以察觉.话不多说,我先用一种极度夸张的手法,描述一下遇到的一个问题.@Componentpublic class ScheduleTest {@Scheduled(initialDelay = 1000,fixedRate = 2*1000)public void test_a(){System.out.println("123"

Flexbox微信踩坑填坑记

Flexbox的大名很早就有了解过,只是之前一直是开发PC端的页面,对这个东西还不是很敢造次去用.近期的项目是移动端开发,正好,内心一激动,就大大咧咧地开始用flexbox布局了.中间踩过的一些坑,以及将来可能还会踩到的坑,都在这里记录一下. 关于flexbox,有一篇文章讲的还是很透彻的,图文并茂,有兴趣的戳链接了解下.一篇很屌很好的文章 在用flexbox之前,很多人最关心的应该还是兼容性的问题.个人一直觉得,前端程序员的使命,就是要推进终端用户去使用最先进的浏览器.当然,现实是骨感的,我们

那些年我们踩过的坑之表单reset

开发者往往是在一个又一个的坑中成长起来的,自学的开发者尤其如此,刚刚填完一个坑,转身又掉进另一个坑.有些坑很容易就跳出来了,也有些坑能整了一天都没头绪,第二天早上一来发现后面就有一架通往坑外的梯子,坑爹啊,问题原来这么简单! 我今天就踩了一个比较坑的坑,好在花了几分钟就跳出来了.事情是这样的: 今天在做一个新增自定义假期的功能,用到了表单.页面上有个新增按钮,点击弹出一个层,里面就是表单,用的是ajax提交.当用户点击取消时(顺便吐槽一下,保存和取消按钮设计得实在是太丑了),清空表单数据,弹出层

《C++之那些年踩过的坑(附录一)》

C++之那些年踩过的坑(附录一) 作者:刘俊延(Alinshans) 本系列文章针对我在写C++代码的过程中,尤其是做自己的项目时,踩过的各种坑.以此作为给自己的警惕. [版权声明]转载请注明原文来自:http://www.cnblogs.com/GodA/p/6639526.html 本来上个月就开始动笔了,直到现在才发出来,实在太多事情.可能有些小朋友不知道写这一篇随笔的起因,那么你可以看一下我之前写的. 上一篇的最后,我提到了一个问题:代码优化.并留了一个小测试:无符号数与有符号数的性能比

【转载】Fragment 全解析(1):那些年踩过的坑

http://www.jianshu.com/p/d9143a92ad94 Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使用姿势3.Fragment之我的解决方案:Fragmentation 本篇主要介绍一些最常见的Fragment的坑以及官方Fragment库的那些自身的BUG,这些BUG在你深度使用时会遇到,比如Fragment嵌套时或者单Activity+多Fragment架构时遇到的坑.如果想看较为实用的技巧,

初学spring boot踩过的坑

一.搭建spring boot环境 maven工程 pom文件内容 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-

用vue-cli搭建项目踩过的坑

1. 需要额外安装sass-loader: 我以为vue-cli会安装所有与css相关的loader,然并非,为这个死了很多脑细胞. npm install sass-loader node-sass webpack --save-dev 2. 修改style标签: 打开src目录下的components目录中的App.vue文件.然后修改 style标签如下: <style lang="sass" rel="stylesheet/sass"> 3. s

多线程和异步编程示例和实践-踩过的坑

上两篇文章,主要介绍了Thread.ThreadPool和TPL 多线程异步编程示例和实践-Thread和ThreadPool 多线程异步编程示例和实践-Task 本文中,分享两则我们在做多线程和异步编程中实际踩过的坑,实际生产环境遇到的问题,以及解决办法. 1. HttpClient 业务场景:使用HttpClient实现第三方业务推送,当第三方的Http服务器不通.或者返回很慢时 线程数暴涨 Asp.Net\Asp.Net MVC场景下,并发多线程导致的线程阻塞:HttpClient.Pos

Fragment全解析系列(一):那些年踩过的坑

Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使用姿势3.Fragment之我的解决方案:Fragmentation 本篇主要介绍一些最常见的Fragment的坑以及官方Fragment库的那些自身的BUG,这些BUG在你深度使用时会遇到,比如Fragment嵌套时或者单Activity+多Fragment架构时遇到的坑.如果想看较为实用的技巧,请直接看第二篇 Fragment是可以让你的app纵享丝滑的设计,如果你的a