码迷,mamicode.com
首页 > 移动开发 > 详细

SpringBoot 项目不加载 application.properties 配置文件

时间:2020-02-04 14:14:29      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:details   ati   href   filter   sources   arc   加载   创建   ring   

起因:新安装的idea第一次运行springboot项目报url错误(Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could be configured.),配置文件application.properties中的代码都是灰色的,而且配置文件的图标也不是绿叶子

技术图片


推测原因是未扫描(没有找到)到这个配置文件

一顿百度之后,借用该帖子(https://www.cnblogs.com/toutou/archive/2019/08/31/embedded-datasource.html)的第四种方法

yml或者properties文件没有被扫描到,需要在pom文件中<build></build>添加如下.来保证文件都能正常被扫描到并且加载成功.
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

加了之后读取到配置文件了但是访问html页面404
去掉这段代码之后再次运行既可以读取到配置文件又可以访问html页面

可能是idea反应过来了

再次创建项目就不会出现这种读取不到配置文件的现象

如果要用到MyBatis可以在pom文件中<build></build>添加如下

  <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  

另外再记录一下yml配置文件不识别的解决方法,博客地址:https://blog.csdn.net/qq_40036754/article/details/100578695

SpringBoot 项目不加载 application.properties 配置文件

标签:details   ati   href   filter   sources   arc   加载   创建   ring   

原文地址:https://www.cnblogs.com/wwct/p/12258984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!