Spring Condition无法从属性文件读取值 [英] Spring Condition not able to read value from property file

查看:541
本文介绍了Spring Condition无法从属性文件读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement Spring Condition org.springframework.context.annotation.Conditionas follows:

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".

However, as shown in the above code, when I read the property "swagger.scanner.can.run" it is always NULL. In my property file I have set it to "swagger.scanner.can.run=false".

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

I also tried to use the @Value("${swagger.scanner.can.run}") but even that returns NULL. When I put a debug in this method I can see that it is being called.

为了完整起见,我按如下方式使用APIScanningDecisionMaker:

Just for the sake of completion I am using the APIScanningDecisionMaker as follows:

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

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

Is there any reason why "swagger.scanner.can.run" is being retrieved as NULL?

推荐答案

也许spring不知道文件?

Maybe spring doesn't know the file ?

可以在使用@Value("${swagger.scanner.can.run}")的带注释的类上解决此问题:

This can be fix on annotated your class where the @Value("${swagger.scanner.can.run}")is used :

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

此致

这篇关于Spring Condition无法从属性文件读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆