温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot加载子模块配置文件的案例分析

发布时间:2020-10-28 11:55:31 来源:亿速云 阅读:453 作者:小新 栏目:编程语言

小编给大家分享一下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: '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).

没有找到数据库的配置文件

找错的过程很痛苦,找了很多资料,走了很多弯路,最后将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加载子模块配置文件的案例分析的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI