【问题标题】:Spring - Profie in external dependencySpring - 外部依赖中的配置文件
【发布时间】:2018-08-02 17:37:04
【问题描述】:

在具有不同配置文件(实际上是 maven 配置文件)的写入依赖项中,我将其包含到我的项目中。如何使用某些属性文件中的属性从该依赖项中选择应该使用哪个配置文件构建我的项目,例如:

spring.profileinmydepedency = prod

你能展示我的一些例子吗,如何在春天做(不是开机)?

【问题讨论】:

    标签: java spring maven properties profiles


    【解决方案1】:

    你不能。

    为一个 maven 模块拥有不同的 maven 配置文件意味着使用特定配置文件构建该项目将产生一个特定的构建 - maven artefact。

    在你的 Spring 项目(也是一个 maven 项目)的 pom.xml 文件中,你声明了对已经构建的 maven artefact 的依赖。

    在 Spring 中,您无法在其中定义属性并将使用该配置文件预构建的二进制文件绑定到您的应用程序。

    您可以采用的最直接的方法是从您的 Spring 项目中的依赖项中复制配置文件。

    例如,如果您的依赖项具有配置文件 P1 和 P2,并且构建工件分别是 dep-p1 和 dep-p2,那么在 Spring 的 pom.xml 中,您将拥有:

    <profiles>
        <profile>
            <id>spring-p1</id>
            …
            <dependencies>
                <dependency>dep-p1</dependency>
            </dependencies>
            …
        </profile>
        <profile>
            <id>spring-p2</id>
            …
            <dependencies>
                <dependency>dep-p2</dependency>
            </dependencies>
            …
        </profile>
    </profiles>
    

    然后,当您使用 spring-p1 配置文件构建项目时,它将包含 dep-p1 依赖项。

    【讨论】:

    • 还有其他方法可以实现吗?也许通过使用弹簧配置文件?
    • 如果你想在 Spring 中基于活动的配置文件创建不同的应用程序上下文,则使用 Spring 配置文件。在您的情况下,依赖项是二进制的,并且是事先创建的。我不知道您的依赖项在做什么或与之关联的 Maven 配置文件。如果依赖项和 maven 配置文件有可能转换为 Spring @ Configuration 和 @ Component 类,那么您可能可以利用 Spring 配置文件。
    猜你喜欢
    • 2017-01-23
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多