Skip to content

Instantly share code, notes, and snippets.

@linxiaobai
Last active June 20, 2019 06:05
Show Gist options
  • Save linxiaobai/29c54dd7c30daf863522aefd89cc0a55 to your computer and use it in GitHub Desktop.
Save linxiaobai/29c54dd7c30daf863522aefd89cc0a55 to your computer and use it in GitHub Desktop.
static class Condition implements ConfigurationCondition {
@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.PARSE_CONFIGURATION;
}
//condition获取properties,直接用context.getEnvironment().getProperty()不行,因为配置还未加载进去,所以需要用以下写法
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Properties properties=new Properties();
Resource resource = context.getResourceLoader().getResource("classpath:/batch.properties");
try {
properties.load(resource.getInputStream());
final String enable = properties.getProperty("batch.enable");
if(enable == null) {
return false;
}
return Boolean.valueOf(enable.trim());
} catch (Exception e) {
logger.error("Exception occurred while batch configuration" , e);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment