Sprint报错:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasour
Sprint报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasour
·
1 背景
项目中总共有两部分含有xml代码
(1)Java路径下XXXMapper.xml,包含增删改查的sql语句
(2)resource路径下的配置文件application.yml和日志记录.xml文件
2 原因分析
(1)maven工程在默认情况下src/main/java目录下的所有资源文件是不会发布到target目录下的,所以为了让Java路径下的xml文件编译到target文件中我使用了如下编译指令:
<build>
<!-- maven工程在默认情况下src/main/java目录下的所有资源文件是不会发布到target目录下的,
因此需要在pom.xml中添加xml配置文件发布配置 -->
<resources>
<resource>
<directory>
src/main/java
</directory>
<includes>
<!-- **表示任意级目录 -->
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
(2)报错显示数据库配置中未找到url参数,所以我猜测resource路径下的资源文件未被读取发布,所以增加如下代码:
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
3 总结
spring boot默认读取resource路径下的配置文件(yml、xml、properties),但当使用<resource>标签指定目录时会覆盖掉默认配置,所以手动配置资源目录让考虑所有的资源文件所在目录,并手动配置
如下:
<build>
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<!-- java路径下的XXXMapper.xml文件-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<!-- resource路径下的配置文件-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
更多推荐
所有评论(0)