【问题标题】:Spring Condition not able to read value from property fileSpring Condition 无法从属性文件中读取值
【发布时间】:2015-12-23 18:06:14
【问题描述】:

我正在尝试如下实现 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 有什么原因吗?

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    也许 spring 不知道文件?

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

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

    问候,

    【讨论】:

    • 实际上,Spring 能够在同一个包中的另一个类中读取相同的属性。但是,该类被注释为@Configuration。但是当我尝试为 APIScanningDecisionMaker 添加相同的注释时,它仍然无法读取属性文件。
    • 你的属性文件在类路径中吗? Web 应用程序的多个 jar 中的部署解决方案,请问您的应用程序的上下文是什么?
    • @PropertySource 值可以使用通配符 *,如下所示:@PropertySource(value="classpath*:config.properties")。在这种情况下,它会在您应用中的每个 ClassLoader 中搜索您的文件
    • 正如我所说,我的其他类能够从同一个文件中读取相同的属性。所以它在类路径中。要回答您的第二个问题,是的 APIScanningDecisionMaker 在另一个 JAR 文件中。此 JAR 文件位于具有 applicationContext.xml 的 WAR 文件中
    • 你知道如果你是WEB-IN/lib目录下的多个jar,每个jar都是由特定的classloader加载的。
    【解决方案2】:

    实现“条件”的类的对象是通过 Spring 的构造函数创建的,因此您不能使用 @Value 注释注入值。您可以在代码中执行以下操作:

    @Override        
    public boolean matches(
    ConditionContext arg0, 
                AnnotatedTypeMetadata arg1) {       
            String prop = conditionContext.getEnvironment()
                     .getProperty("arg0");   
    //further code   
         }    
    

    【讨论】:

      【解决方案3】:

      如果您将@Conditional 添加到配置类,那么APIScanningDecisionMaker 应该实现ConfigurationCondition。并且不要忘记将@PropertySource 添加到配置类。

      import org.springframework.context.annotation.ConfigurationCondition;
      
      public class APIScanningDecisionMaker implement ConfigurationCondition {
          @Override
          public ConfigurationPhase getConfigurationPhase() {
              return ConfigurationPhase.REGISTER_BEAN;
          }
      }
      

      属性将在ConfigurationPhase.REGISTER_BEAN 阶段加载。

      如果您将@Conditional 用于方法,那么您可以实现Condition

      【讨论】:

        【解决方案4】:

        您可以手动加载属性文件,如果由于任何问题,它在构建条件时没有加载到 spring 的上下文中。

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
          try {
              Properties appProps = new Properties();
        appProps.load(context.getResourceLoader().getResource("classpath:app.properties").getInputStream());
              String canRunFlagInStr = appProps.getProperty("swagger.scanner.can.run");
              return Boolean.parseBoolean(canRunFlagInStr);
          } catch (IOException e) {
              throw new ApplicationContextException(("Exception while loading context", e);
          }
        }
        

        【讨论】:

          【解决方案5】:

          这对我来说很好用

          public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
              try {
                  InputStream i = conditionContext.getResourceLoader().getResource("prop.file").getInputStream();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(i));
                  StringBuilder out = new StringBuilder();
                  String line;
                  while ((line = reader.readLine()) != null) {
                      out.append(line);
                  }
                  System.out.println(out.toString());
          
              } catch (IOException e) {
                  e.printStackTrace();
              }
          
              return true;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-02-14
            • 2013-10-25
            相关资源
            最近更新 更多