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中的代码都是灰色的,而且配置文件的图标也不是绿叶子

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

一顿百度之后,借用该帖子(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

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

时间: 2024-08-30 08:19:09

SpringBoot 项目不加载 application.properties 配置文件的相关文章

springboot不加载bootstrap.properties文件

1.首先说一下官网对bootstrap和application两种配置文件的区别: Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton.bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性.这两个上下文共用一个环境,它是任何Spri

Java 读取application.properties配置文件中配置

实际开发中若需要读取配置文件application.properties中的配置,代码如下.例:读取配置文件中name属性配置值: 代码如下: import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.u

springboot配置之使用application.properties时编码问题

上一节我们是在application.yml文件中进行配置,本节我们在application.properties中进行配置. application.properties person.username=张三 person.age=12 [email protected] person.maps.k1=v1 person.maps.k2=v2 person.lists=a,b,c person.dog.name=tom person.dog.age=2 其它配置不变,运行测试: Person{

SpringBoot通过@Value获取application.yml配置文件的属性值

application.yml实例: spring: redis: database: 0 host: 127.0.0.1 获取方法: /** * @Auther:WangZiBin * @Description: * @Modified By: */ @Configuration public class JedisConfig{ private Logger jedisConfigLogger= LoggerFactory.getLogger(JedisConfig.class); @Val

在SpringBoot下读取自定义properties配置文件的方法

SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 1 2 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/reso

微服务之springboot 自定义配置(一)Application.properties文件

配置的文件的格式 springboot可以识别两种格式的配置文件,分别是yml和properties 文件.我们可以将application.properties文件换成application.yml,这两个文件都可以被SpringBoot自动识别并加载,但是如果是自定义的配置文件,就最好还是使用properties格式的文件,因为SpringBoot中暂时还并未提供手动加载yml格式文件的功能(这里指注解方式). yml 配置文件 属性格式:配置的属性和属性值要有空格隔开.没有空格报:java

Spring Boot2 系列教程(四)理解Spring Boot 配置文件 application.properties

在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,yaml 更加简洁明了,而且使用的场景也更多,很多开源项目都是使用 yaml 进行配置(例如 Hexo).除了简洁,yaml 还有另外一个特点,就是 yaml 中的数据是有序的,properties 中的数据是无序的,在一些需要路径匹配的配置中,顺序就显得尤为重要(例如我们在 Spring Cloud

springBoot(2):Properties和YAML配置文件

一.配置文件的生效顺序,会对值进行覆盖 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环境变量 5. 只有在random.*里包含的属性会产生一个RandomValuePropertySource 6. 在打包的jar外的应用程序配置文件(application.properties,包含YAML和profile变量) 7. 在打包的jar内的应用程序配置文件(application.p

application.properties和application.yml文件的区别

关于 一般上来说,当我们创建一个SpringBoot项目时,IDE会默认帮我们创建一个application.properties配置文件.有些朋友习惯把.properties文件改成.yml文件.那么这两种文件类型有什么区别呢? 区别 1.内容格式比较: .properties文件,通过.来连接,通过=来赋值,结构上,没有分层的感觉,但比较直接. .yml文件,通过:来分层,结构上,有比较明显的层次感,最后key赋值的:后需要留一个空格 2.执行顺序 如果工程中同时存在application.