这两天最先进修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加载子模块设置文件的细致引见(代码示例)的细致内容,更多请关注ki4网别的相干文章!