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,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...
随机推荐
- “全栈2019”Java第三十四章:可变参数列表
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- php数据库编程---mysql扩展库
1, Java有一种方式操作数据库, PHP有三种方式来操作mysql数据库.(1)mysql扩展库:(2)mysqli扩展库:(3)pdo: 2, mysql扩展库和mysql数据库区别 3, my ...
- PHP内核研究 静态变量
静态变量 它可以是 静态全局变量,如果不调用unset,那么这个静态变量会一直存在,直到程序退出时才由Zend内存管理来释放 它可以是 静态局部变量:在函数里定义,函数执行完后,该静态变量不会消失 它 ...
- Django 允许其他可以访问的设置
第一步:在run下修改edit 第二步:host改为0.0.0.0 第三步:setting文件中将 ALLOWED_HOSTS 改为 :ALLOWED_HOSTS = ['*',] 这样就可以通 ...
- Your branch is ahead of 'origin/master' by 1 commit.
git reset HEAD^ --soft git reset HEAD^ --hard --soft 表示保留当前commit,重新commit --hard 表示丢弃当前add,重新add.co ...
- ArchLinux下Shell基础学习
首先来认识脚本语言:通常指的是命令行界面的解析器.(来自维基的解释) 第一部分:认识Shell 大家可以看到这里使用了#!/bin/sh和!/bin/bash.可是俩者有什么区别呢?下图有解释. sh ...
- history.back返回是浏览器错误码:ERR_CACHE_MISS
解决方法: 如果访问的是php文件中添加:header("Cache-control: private"); 如果使用的是模板引擎(tp5):{php}header("C ...
- docker 容器container运行后,如何进入容器内部?
docker exec -ti 容器ID /bin/bash
- SqlServer数据库《基本》
数据库简单说就是行.列组成的二维表 把列称为字段.每一行数据成为记录,能标识每一行的唯一字段称为主键 查询数据表时,索引可以提高查询速度,但是索引同时会降低新增和更新数据时的速度,应为还要更新索引. ...
- dynamics crm 365 附件上传图片并且显示。
参考了几篇博客做的: 新增websource文件(html): <!DOCTYPE html> <html> <head> <title>注释</ ...