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用;使用它,却不用它的新特性,真的是毫无意义啊.
参考文献:
https://blog.csdn.net/cw_hello1/article/details/79639448
SpringBoot加载子模块配置文件的方法的更多相关文章
- springboot加载外部配置文件
网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...
- 【java编程】加载Resources配置文件的方法
一.加载配置实现k-v //FILTER_PROPERTY_NAME=META-INF/abc.properties private void loadFilterConfig(Properties ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- Springboot 加载配置文件源码分析
Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- selenium启动Chrome时,加载用户配置文件
selenium启动Chrome时,加载用户配置文件 Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...
- WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。","ExceptionType":"System.Security.Cryptography.CryptographicException","StackTrace
在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: ...
- 使用Spring加载properties配置文件.md
背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...
- spring加载属性配置文件内容
在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...
随机推荐
- (1)python的基础认知
Python程序员的信仰:人生苦短,我用python! (一)python的发展史 1989年开发的语言,创始人范罗苏姆(Guido van Rossum),别称:龟叔(Guido).为了打发圣诞节假 ...
- 实验1 C语言运行环境的使用和数据类型、运算符和表达式
Part1 这一部分的内容虽然简单,但是对于初学的我来说,独自完成且没有错误还是不容易的,像老师说的一样,只有自己亲手编写以后才可以发现问题并且逐步改正.从这次实践我对与C语言程序的结构更加熟悉. ...
- freemarker模板跟java代码放在一起
在配置 freemarkerConfig时加上 <property name="templateLoaderPath" value="classpath:hello ...
- UDP与IP分片
一.引言  UDP是简单的面向数据报的运输层协议.UDP不提供可靠性:它负责把应用程序传给IP层的数据发送出去,不保证能导到目的地. 二.UDP首部  端口号表示发送进程和接受进程.由于IP层已经 ...
- NSUserDefault存储类
类要实现NSCoding协议 实现encodeWithCoder和initWithCoder方法 先把类的instance archive成NSData,然后存起来 TGTest *tt = [TGT ...
- windows10下使用ubuntu,并搭建nodejs环境
1.首先要在win10的设置里打开开发人员使用模式 2.在bash下,下载ubuntu系统 3.安装git,因为要把下载nvm的话,需要git sudo apt-get install git 4.安 ...
- Saiku2.6 Saiku315 链接SQL的JDBC字符串
Saiku26 type=OLAP name=CloudConn driver=mondrian.olap4j.MondrianOlap4jDriver location=jdbc:mondrian: ...
- 1095. Maximum Swap —— Weekly Challenge
题目限定输入是[0, 10^8],因而不用考虑负数或者越界情况,算是减小了难度. public class Solution { /** * @param num: a non-negative in ...
- Linux平台总线设备驱动
1. 平台总线(Platform bus)是linux2.6内核加入的一种虚拟总线,其优势在于采用了总线的模型对设备(没有挂到真实总线的设备)与驱动进行了管理,这样提高了程序的可移植性. 2. 平台总 ...
- XNA 中3D模型的显示
XNA 中3D模型的显示: ModelMeshPart[] meshParts; Model start_model; Matrix[] dq_model_transforms; Matrix vie ...