【问题标题】:Spring boot 2.0 - configuration for usersSpring boot 2.0 - 用户配置
【发布时间】:2018-08-09 15:10:20
【问题描述】:

我有一个应用程序(springboot 2),客户想要编辑整数值,它代表 AutoGrowCollectionLimit 的最大值。这个值默认(根据 springdocs)设置为 256,这对于我们的目的来说是不够的。

设置属性的代码:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.setAutoGrowCollectionLimit([configurable_number]);
}

这个值应该可以在配置文件中进行配置(比如说 some.txt),它将作为一个 txt 文件传递​​到应用程序旁边。现在将 some.txt 文件放在哪里都没有关系,现在即使是应用程序的 root 也可以。

这意味着,作为客户,我可以轻松更改它。打开 some.txt 文件并将值从 i.e.: 256 更改为 i.e.: 555.

在调查期间,我找到了this。但它不适合我的情况。我正在寻找的是 some.txt 文件中具有非常简单属性的配置,即:

AutoGrowCollectionLimit=[configurable_number]

根据springdocs,我尝试了以下:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.setAutoGrowCollectionLimit(${set.max.collectionLimit});
}

并且还编辑了 [projectUrl]/src/main/resources/application.yml 如下:

set:
    max:
     collectionLimit: 500

当我尝试在以下位置调用此属性时,IDE 需要 ')' 或 '}':

binder.setAutoGrowCollectionLimit(${set.max.collectionLimit});

有人可以帮忙吗?

【问题讨论】:

  • 这远非一个正确的问题。您应该从编写一些代码开始。
  • 好的,我编辑了问题,现在更好了吗?
  • 好多了,是的。在等待重新打开问题时,请查看 Spring Externalized Configuration 文档,特别是有关在类路径之外提供其他 .yml 或 .properties 文件的信息。

标签: java spring-boot configuration yaml


【解决方案1】:

在 Spring Boot 中有多种方法可以创建外部化配置,但您需要使用@Value 注入来实现几乎所有这些。

值注入

要注入您的配置值,您需要使用@Value 注释。这可以在您可以使用@Autowired 的所有相同位置完成。例如,在一个属性上:

@Value("${com.example.app.host-name}")
private String hostName;

或者通过构造函数或方法:

@Value("${com.example.app.host-name}")
public void setHostName(String hostName) { ... }

或者在特定的构造函数或方法参数上:

public MyServiceBean(
        @Value("${com.example.app.host-name}") String hostName,
        @Value("${com.example.app.port}") int port) {
    ...
}

您还可以使用此系统使用: 符号提供默认值,例如:

@Value("${com.example.app.port:8180}")
public void setPort(int port) { ... }

所有这些中的${} 位都是使用Spring Expression Language 解释的。在这种情况下,${property} 语法告诉 Spring 从上下文中检索 property 的值,这是通过在上下文中的所有 PropertySource bean 中查找属性来完成的。您也可以通过上下文使用Environment.getProperty 自己执行此操作,例如:

ApplicationContext appCtx = ... ;
int port = appCtx.getEnvironment().getProperty("com.example.app.port", Integer.class);

使用@Value注解更方便,原因与使用@Autowired更方便的原因相同

Spring Boot 配置

由于您使用的是 Spring Boot,因此您的应用中已经有一些 PropertySource 实例。例如,您的application.yml 文件被加载为PropertySource。请注意,Spring 会将名为 a.b.c 的属性转换为 YAML 文件中的嵌套文档。在您的情况下,这将是 set.max.collectionLimit

Spring Boot 通过查找application.ymlapplication.properties 文件以及System.getProperties() 等其他属性来源来执行此操作,并且可以找到Spring Boot 查找这些属性的默认顺序here in the documentation

外部配置

要外部化您的配置,您声明您不想使用 .yml 文件,但是 .properties 呢?例如:

set.max.collectionLimit=555

您可以将此文件放在 .jar 文件旁边,并将其命名为 application.properties。此文件中的任何值都将覆盖您在内部 application.yml 文件中的值。

他们也可以override it directly on the command line,例如:

java -jar your-app.jar --set.max.collectionLimit=555

或者通过System属性:

java -Dset.max.collectionLimit=555 -jar your-app.jar

所有这些都是覆盖值的有效方法,但前提是您使用值注入,例如通过@Value

【讨论】:

    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 2021-12-11
    • 2018-10-07
    • 2019-02-17
    • 2020-05-02
    • 2014-11-30
    • 2022-01-21
    • 2013-11-01
    相关资源
    最近更新 更多