springboot踩坑出坑記

4月15到4月17我都在把畢設從eclipse重構到IDEA中,springboot最讓我頭疼的是它的版本問題,由於每個版本對應的依賴包都有可能出錯,這裏分享一下如何成功移植用eclipse寫的springboot到IDEA中,比較簡單的步驟我這裏不詳細說了,說一下我遇到的一些很難找出問題的地方 ps:只是針對於個人項目和我我的水平,大神勿噴嘿嘿html

<!--more-->java

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之類的種種,但沒掃到,好吧~

解決方法:mysql

  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文件,我就一次性都配上了,這個你們具體遇到的時候再去搜一下就行,不用刻意的記:git

<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......github

以後再pom.xml裏面會看到Idea自動爲你引入的依賴和 spring-boot-maven-plugin 插件,插件版本我建議仍是稍微低一點,由於boot真的是隨着版本變更改動很大,我用的是web

<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得本身找...redis

<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...spring

  • 以後配置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中報錯了:sql

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裝配,再用...:apache

@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

相關文章
相關標籤/搜索