1. 程式人生 > >springboot踩坑出坑記

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>

這就是我移植專案遇到的一些問題,下面列一些大佬的部落格,對我幫助很大,不勝感激
如有侵權,請聯絡我刪除

  • springboot @WebFilter過濾器的使用
  • java.lang.AbstractMethodError: null
  • 不能自動裝配RestTemplate / not found
  • SpringBoot 2.0 報錯: Failed to configure a DataSource: 'url' attribute is not specified and no embe...
  • 在pom.xml檔案中使用resources外掛的小作用