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

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

SpringBoot 项目不加载 application.properties 配置文件_第1张图片


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

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

yml或者properties文件没有被扫描到,需要在pom文件中添加如下.来保证文件都能正常被扫描到并且加载成功.


    
        src/main/java
        
            **/*.yml
            **/*.properties
            **/*.xml
        
        false
    
    
        src/main/resources
        
            **/*.yml
            **/*.properties
            **/*.xml
        
        false
    

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

可能是idea反应过来了

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

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

  
        
            
                src/main/java
                
                    **/*.xml
                
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

  

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

你可能感兴趣的:(SpringBoot 项目不加载 application.properties 配置文件)