SpringBoot加載子模塊配置文件的案例分析-創(chuàng)新互聯(lián)

小編給大家分享一下SpringBoot加載子模塊配置文件的案例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的定邊網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

這兩天開始學(xué)習(xí)SpringBoot框架,按照官方的文檔,很輕易地就把單模塊的項(xiàng)目啟動(dòng)了,但在使用maven搭建多模塊的時(shí)候遇到了子模塊配置文件沒(méi)有加載的問(wèn)題

項(xiàng)目架構(gòu)是這樣的

    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
給啟動(dòng)器類加注解
@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);
    }
}
運(yùn)行

然后是喜聞樂(lè)見的報(bào)錯(cuò)

***************************
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).

沒(méi)有找到數(shù)據(jù)庫(kù)的配置文件

找錯(cuò)的過(guò)程很痛苦,找了很多資料,走了很多彎路,最后將dao層的application.yml全部剪切到ws的application.yml中,項(xiàng)目點(diǎn)亮,成功運(yùn)行.這就明確到dao層的配置文件沒(méi)有被加載.

然后找到了加載配置文件的方法:

我將ws層application.yml重命名為application-dev.yml;將dao層配置文件重命名為application-dao.yml(讓配置文件不重名,需要注意,配置文件重命名后必須以application-做前綴);

在ws層再新建一個(gè)application.yml

spring:
  profiles:
    active: dao,dev

這條配置是指定加載哪些配置文件

操作完成,系統(tǒng)成功點(diǎn)亮

本來(lái)是很簡(jiǎn)單的問(wèn)題,卻浪費(fèi)了我很長(zhǎng)的時(shí)間,還有一件事情必須要吐槽一下,現(xiàn)在SpringBoot相關(guān)的博客質(zhì)量真是良莠不齊,相當(dāng)數(shù)量的人還是把它當(dāng)SpringMvc用;使用它,卻不用它的新特性,真的是毫無(wú)意義啊.

以上是SpringBoot加載子模塊配置文件的案例分析的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章名稱:SpringBoot加載子模塊配置文件的案例分析-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article12/csjddc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管品牌網(wǎng)站制作、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)企業(yè)網(wǎng)站制作、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)