gpt4 book ai didi

java - Spring Condition 无法从属性文件中读取值

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:26:24 24 4
gpt4 key购买 nike

我正在尝试实现 Spring Condition org.springframework.context.annotation.Condition,如下所示:

public class APIScanningDecisionMaker implements Condition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

// Not able to read the property "swagger.scanner.can.run". It is always NULL.
String canRunFlagInStr = context.getEnvironment().getProperty("swagger.scanner.can.run");
// Ignore the rest of the code.
}
}

但是,如上面的代码所示,当我读取属性“swagger.scanner.can.run”时,它始终为 NULL。在我的属性文件中,我将其设置为“swagger.scanner.can.run=false”。

我还尝试使用 @Value("${swagger.scanner.can.run}") 但即使这样也会返回 NULL。当我在此方法中进行调试时,我可以看到它正在被调用。

为了完成,我使用 APIScanningDecisionMaker 如下:

@Configuration
@EnableSwagger
@Conditional(APIScanningDecisionMaker.class)
public class CustomSwaggerConfig {
// Ignore the rest of the code
}

“swagger.scanner.can.run”被检索为 NULL 是否有任何原因?

最佳答案

也许 spring 不知道这个文件?

这可以修复使用 @Value("${swagger.scanner.can.run}") 的注释类:

@PropertySource(value="classpath:config.properties")

问候,

关于java - Spring Condition 无法从属性文件中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com