Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何覆盖Spring启动库属性?_Java_Spring Boot - Fatal编程技术网

Java 如何覆盖Spring启动库属性?

Java 如何覆盖Spring启动库属性?,java,spring-boot,Java,Spring Boot,我想覆盖我在使用它的项目中编写的库属性。我正在使用此Spring指南创建库: 我想知道如何在使用我的库的项目中覆盖例如my.properties文件 这可能吗?看来你不可能。既然春天本身也不能 在上面的示例中,我们使用@SpringBootTest注释的默认属性为测试配置了service.message。不建议将application.properties放入库中,因为使用它的应用程序在运行时可能会发生冲突(从类路径仅加载一个application.properties)。您可以将applica

我想覆盖我在使用它的项目中编写的库属性。我正在使用此Spring指南创建库:

我想知道如何在使用我的库的项目中覆盖例如my.properties文件


这可能吗?

看来你不可能。既然春天本身也不能

在上面的示例中,我们使用@SpringBootTest注释的默认属性为测试配置了service.message。不建议将application.properties放入库中,因为使用它的应用程序在运行时可能会发生冲突(从类路径仅加载一个application.properties)。您可以将application.properties放在测试类路径中,但不能将其包括在jar中,例如,将其放在src/test/resources中


看来你不能。既然春天本身也不能

在上面的示例中,我们使用@SpringBootTest注释的默认属性为测试配置了service.message。不建议将application.properties放入库中,因为使用它的应用程序在运行时可能会发生冲突(从类路径仅加载一个application.properties)。您可以将application.properties放在测试类路径中,但不能将其包括在jar中,例如,将其放在src/test/resources中


不要将application.properties文件添加到库中。它可能会引起很多问题。如果要设置默认属性,请执行以下操作:

@ConfigurationProperties("foo")
public class FooConfig {

    private int bar = 999;

    // getter / setter

}

每个应用程序都应该在自己的application.properties文件中为自己配置库值。

不要将application.properties文件添加到库中。它可能会引起很多问题。如果要设置默认属性,请执行以下操作:

@ConfigurationProperties("foo")
public class FooConfig {

    private int bar = 999;

    // getter / setter

}

每个应用程序都应该在自己的application.properties文件中为自己配置库值。

这里介绍了Spring Boot属性顺序优先级:

从链接:

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

那么顺序是1-17。例如,列表中的4个(命令行参数)覆盖列表中的15个(application.properties文件),依此类推。

Spring启动属性顺序优先级如下所述:

从链接:

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

那么顺序是1-17。例如,列表中的4(命令行参数)覆盖列表中的15(application.properties文件),依此类推。

取决于库属性文件指定属性的方式。如果首先从环境/系统中查找属性,则可以在代码中重写。如果是硬编码,则不是。 例如:

prop=${ENV_VAR:abc}

您可以在代码中设置
ENV_VAR
环境变量,或
ENV VAR
系统变量,以覆盖
prop
的值。如果没有,将使用默认值
abc

取决于库属性文件指定属性的方式。如果首先从环境/系统中查找属性,则可以在代码中重写。如果是硬编码,则不是。 例如:

prop=${ENV_VAR:abc}

您可以在代码中设置
ENV_VAR
环境变量,或
ENV VAR
系统变量,以覆盖
prop
的值。如果不这样做,将使用默认值
abc

“不要将属性文件添加到库”-通常不正确。如果你知道自己在做什么,那就没什么问题。@AbhijitSarkar,那件事是你告诉我的。在回答中修复了它。“不要将属性文件添加到库”-一般不正确。如果你知道自己在做什么,那就没什么问题。@AbhijitSarkar,那件事是你告诉我的。在答案中修正了它。