Add placeholder resolution to OnResourceCondition
Users can write @ConditionalOnResource("${path.to.file}") and
placeholders are resolved from the environment.
This commit is contained in:
@@ -50,7 +50,7 @@ class OnResourceCondition extends SpringBootCondition {
|
||||
Assert.isTrue(locations.size() > 0,
|
||||
"@ConditionalOnResource annotations must specify at least one resource location");
|
||||
for (String location : locations) {
|
||||
if (!loader.getResource(location).exists()) {
|
||||
if (!loader.getResource(context.getEnvironment().resolvePlaceholders(location)).exists()) {
|
||||
return ConditionOutcome.noMatch("resource not found: " + location);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,15 +16,16 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConditionalOnResource}.
|
||||
*
|
||||
@@ -42,6 +43,15 @@ public class ConditionalOnResourceTests {
|
||||
assertEquals("foo", this.context.getBean("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourceExistsWithPlaceholder() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "schema=schema.sql");
|
||||
this.context.register(PlaceholderConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertTrue(this.context.containsBean("foo"));
|
||||
assertEquals("foo", this.context.getBean("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourceNotExists() {
|
||||
this.context.register(MissingConfiguration.class);
|
||||
@@ -66,4 +76,13 @@ public class ConditionalOnResourceTests {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnResource(resources = "${schema}")
|
||||
protected static class PlaceholderConfiguration {
|
||||
@Bean
|
||||
public String foo() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user