首頁 >Java >java教程 >SpringBoot載入子模組設定檔的詳細介紹(程式碼範例)

SpringBoot載入子模組設定檔的詳細介紹(程式碼範例)

不言
不言轉載
2019-02-16 13:47:485000瀏覽

本篇文章帶給大家的內容是關於SpringBoot載入子模組設定檔的詳細介紹(程式碼範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

這兩天開始學習SpringBoot框架,按照官方的文檔,很輕易地就把單模組的專案啟動了,但在使用maven搭建多模組的時候遇到了子模組設定檔沒有載入的問題

專案架構是這樣的

    zero
        |-ws
            |-service
                |-dao
                    |-entity

zero的依賴

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

ws的依賴與設定

<dependencies>
    <dependency>
      <groupId>cn.xmcui.zero</groupId>
      <artifactId>service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!--指定该class为全局唯一入口-->
          <mainClass>cn.xmcui.zero.Application</mainClass>
          <fork>true</fork>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

ws的application.yml

server:
  port: 80
  servlet:
    session:
      timeout: 60
  tomcat:
    uri-encoding: utf-8

dao的依賴與設定

<dependencies>
    <dependency>
      <groupId>cn.xmcui.zero</groupId>
      <artifactId>entity</artifactId>
      <version>1.0-SNAPSHOT</version>
    </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.47</version>
    </dependency>
  </dependencies>

application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/zero?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: cn.xmcui.zero.entity

給啟動器類別加註解

@SpringBootApplication(scanBasePackages = "cn.xmcui.zero")
@MapperScan(basePackages = "cn.xmcui.zero.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

執行

然後是喜聞樂見的報錯

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: &#39;url&#39; 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).

沒有找到資料庫的設定檔

找錯的過程很痛苦,找了很多資料,走了很多彎路,最後將dao層的application.yml全部剪切到ws的application .yml中,專案點亮,成功運行.這就明確到dao層的設定檔沒有被載入.

然後找到了載入設定檔的方法:

我將ws層application .yml重新命名為application-dev.yml;將dao層設定檔重命名為application-dao.yml(讓設定檔不重名,需要注意,設定檔重命名後必須以application-做前綴);

在ws層再新建一個application.yml

spring:
  profiles:
    active: dao,dev

這條設定是指定載入哪些設定檔

操作完成,系統成功點亮

本來是很簡單的問題,卻浪費了我很長的時間,還有一件事情必須要吐槽一下,現在SpringBoot相關的博客質量真是良莠不齊,相當數量的人還是把它當SpringMvc用;使用它,卻不用它的新特性,真的是毫無意義啊.

以上是SpringBoot載入子模組設定檔的詳細介紹(程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除