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

Raj*_*Raj 9 java spring spring-mvc

我试图实现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.
   }
}
Run Code Online (Sandbox Code Playgroud)

但是,如上面的代码所示,当我读取属性"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
}
Run Code Online (Sandbox Code Playgroud)

是否有任何理由将"swagger.scanner.can.run"检索为NULL?

小智 1

也许 spring 不知道该文件?

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

@PropertySource(value="classpath:config.properties")
Run Code Online (Sandbox Code Playgroud)

问候,