Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the situation of specifying spring.application.name in the bootstrap file after introducing spring cloud #9801

Merged
merged 10 commits into from
Nov 6, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public Resource createResource(ConfigProperties config) {
// that we have "first one wins" while Spring has "last one wins".
// The docs for Spring are here:
// https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
// https://docs.spring.io/spring-cloud-commons/docs/4.0.4/reference/html/#the-bootstrap-application-context
Stream<Supplier<String>> finders =
Stream.of(
this::findByCommandlineArgument,
Expand All @@ -84,7 +85,10 @@ public Resource createResource(ConfigProperties config) {
this::findByCurrentDirectoryApplicationYaml,
this::findByClasspathApplicationProperties,
this::findByClasspathApplicationYml,
this::findByClasspathApplicationYaml);
this::findByClasspathApplicationYaml,
this::findByClasspathBootstrapProperties,
laurit marked this conversation as resolved.
Show resolved Hide resolved
this::findByClasspathBootstrapYml,
this::findByClasspathBootstrapYaml);
return finders
.map(Supplier::get)
.filter(Objects::nonNull)
Expand Down Expand Up @@ -136,6 +140,14 @@ private String findByClasspathApplicationProperties() {
return result;
}

@Nullable
private String findByClasspathBootstrapProperties() {
String result = readNameFromBootstrapProperties();
logger.log(
FINER, "Checking for spring.application.name in bootstrap.properties file: {0}", result);
return result;
}

@Nullable
private String findByCurrentDirectoryApplicationProperties() {
String result = null;
Expand All @@ -153,11 +165,21 @@ private String findByClasspathApplicationYml() {
return findByClasspathYamlFile("application.yml");
}

@Nullable
private String findByClasspathBootstrapYml() {
return findByClasspathYamlFile("bootstrap.yml");
}

@Nullable
private String findByClasspathApplicationYaml() {
return findByClasspathYamlFile("application.yaml");
}

@Nullable
private String findByClasspathBootstrapYaml() {
return findByClasspathYamlFile("bootstrap.yaml");
}

private String findByClasspathYamlFile(String fileName) {
String result = loadFromClasspath(fileName, SpringBootServiceNameDetector::parseNameFromYaml);
if (logger.isLoggable(FINER)) {
Expand Down Expand Up @@ -262,6 +284,12 @@ private String readNameFromAppProperties() {
"application.properties", SpringBootServiceNameDetector::getAppNamePropertyFromStream);
}

@Nullable
private String readNameFromBootstrapProperties() {
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
return loadFromClasspath(
"bootstrap.properties", SpringBootServiceNameDetector::getAppNamePropertyFromStream);
}

@Nullable
private static String getAppNamePropertyFromStream(InputStream in) {
Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
@ExtendWith(MockitoExtension.class)
class SpringBootServiceNameDetectorTest {

static final String PROPS = "application.properties";
static final String APPLICATION_PROPS = "application.properties";
static final String APPLICATION_YML = "application.yml";

static final String BOOTSTRAP_PROPS = "bootstrap.properties";

static final String BOOTSTRAP_YML = "bootstrap.yml";

@Mock ConfigProperties config;
@Mock SystemHelper system;

Expand All @@ -48,18 +53,28 @@ void findByEnvVar() {

@Test
void classpathApplicationProperties() {
when(system.openClasspathResource(PROPS)).thenReturn(openClasspathResource(PROPS));
when(system.openClasspathResource(APPLICATION_PROPS))
.thenReturn(openClasspathResource(APPLICATION_PROPS));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "dog-store");
}

@Test
void classpathBootstrapProperties() {
when(system.openClasspathResource(BOOTSTRAP_PROPS))
.thenReturn(openClasspathResource(BOOTSTRAP_PROPS));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "dog-store-bootstrap");
}

@Test
void propertiesFileInCurrentDir() throws Exception {
Path propsPath = Paths.get(PROPS);
Path propsPath = Paths.get(APPLICATION_PROPS);
try {
writeString(propsPath, "spring.application.name=fish-tank\n");
when(system.openFile(PROPS)).thenCallRealMethod();
when(system.openFile(APPLICATION_PROPS)).thenCallRealMethod();
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "fish-tank");
Expand All @@ -77,6 +92,25 @@ void classpathApplicationYaml(String fileName) {
expectServiceName(result, "cat-store");
}

@ParameterizedTest
@ValueSource(strings = {"bootstrap.yaml", BOOTSTRAP_YML})
void classpathBootstrapYaml(String fileName) {
when(system.openClasspathResource(fileName)).thenReturn(openClasspathResource(BOOTSTRAP_YML));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store-bootstrap");
}

@ParameterizedTest
@ValueSource(strings = {"bootstrap.yaml", BOOTSTRAP_YML})
void classpathBootstrapYamlContainingMultipleYamlDefinitions(String fileName) {
when(system.openClasspathResource(fileName))
.thenReturn(ClassLoader.getSystemClassLoader().getResourceAsStream("bootstrap-multi.yml"));
SpringBootServiceNameDetector guesser = new SpringBootServiceNameDetector(system);
Resource result = guesser.createResource(config);
expectServiceName(result, "cat-store-bootstrap");
}

@ParameterizedTest
@ValueSource(strings = {"application.yaml", APPLICATION_YML})
void classpathApplicationYamlContainingMultipleYamlDefinitions(String fileName) {
Expand Down
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: cat-store-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name: dog-store-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: cat-store-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: cat-store-bootstrap
Loading