本篇文章给大家带来的内容是关于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加载子模块配置文件的详细介绍(代码示例)的详细内容。更多信息请关注PHP中文网其他相关文章!

Docker Nginx部署前端项目遇到的空白页及代理异常问题在使用Docker和Nginx部署前后端分离项目时,经常会遇到一些�...

SpringBoot3项目外部配置文件的部署方法在SpringBoot3项目开发中,我们经常需要将配置文件application.properties...

将Apache的.htaccess配置转换为Nginx的配置方法在项目开发中,经常会遇到需要将服务器从Apache迁移到Nginx的情况。Ap...

JavaWeb应用性能优化:Dao层实体类缓存的可行性探讨在JavaWeb应用开发中,性能优化一直是开发者关注的重点。尤�...

在高并发环境下如何保证脚本任务的唯一性和监控其运行状态?本文将探讨如何在集群环境中,确保一个出库脚...

关于子类如何通过继承父类的setName方法设置私有属性在编程中,特别是在面向对象编程的语言如Java中,子类与�...

如何解决使用EclipsePaho的MqttAsyncClient连接本地EMQX时用户名密码认证失败的问题?在使用Java和Eclipse...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具