欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot加载子模块配置文件的方法

程序员文章站 2022-12-16 09:55:45
这两天开始学习SpringBoot框架,按照官方的文档,很轻易地就把单模块的项目启动了,但在使用maven搭建多模块的时候遇到了子模块配置文件没有加载的问题 项目架构是这样的 zero的依赖 ws的依赖和配置 ws的application.yml dao的依赖和配置 application.yml ......

这两天开始学习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用;使用它,却不用它的新特性,真的是毫无意义啊.

参考文献:
https://blog.csdn.net/cw_hello1/article/details/79639448