【问题标题】:How to change Spring Boot properties default path in Tomcat development environment如何在Tomcat开发环境中更改Spring Boot属性默认路径
【发布时间】:2018-05-03 11:09:04
【问题描述】:

我需要使用两个*.properties 文件来确定Spring Boot 应用服务器的配置。如何设置第二个配置路径?

我使用spring-boot-starter-parent版本1.5.10*.properties文件:

spring.datasource.url=url
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialec
spring.jpa.properties.hibernate.current_session_context_class=
org.springframewok.orm.hibernate4.SpringSessionContext
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=30000

现在,我需要从另一个属性文件中处理数据库信息。

编辑:请注意,我需要将第二个属性文件放在我的 WAR 文件之外的 WEB-INF 文件夹中。

【问题讨论】:

    标签: java spring spring-boot properties-file


    【解决方案1】:

    (1)这是在开发模式和生产环境之间切换时的最佳做法。

    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles

    参考版本1.5.10.RELEASEhttps://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-external-config-profile-specific-properties

    更具体地说,您创建 3 个文件

    application.properties 用于常用属性。

    application-dev.properties 仅用于配置文件dev 中使用的自有属性

    application-production.properties 仅用于配置文件production 中使用的自有属性

    (注意:命名有约定)

    application.properties 中使用的点配置文件有spring.profiles.active=dev(开发中)或spring.profiles.active=production(生产中)行

    (2)

    请注意,我需要将第二个属性文件放在我的 WAR 文件之外 在 WEB-INF 文件夹中。

    假设,您的文件是foo.properties。它在 WAR 文件之外,它没有 Spring 的属性文件的性质。因此,Spring Framework/Spring Boot 无法自动读取。您必须编写几行 Java 代码来读取foo.properties 的内容,然后手动分配给配置(参见示例configuration class

    【讨论】:

    • 我只是编辑我的问题,请根据该注释给我建议
    • 请记住:文件*.properties 外部WAR 文件,它不是Spring 的属性。
    • 好的,谢谢我知道了。现在我应该搜索如何从代码中设置属性
    • 考虑使用spring.config.location(Java 代码配置的替代方案)参考:docs.spring.io/spring-boot/docs/current/reference/html/…
    【解决方案2】:

    您可以使用@PropertySource访问多个属性文件

    关注以下 stackoverflow 线程。 https://stackoverflow.com/a/47178674/7538821

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多