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 @@ -130,10 +134,12 @@ private String findBySystemProperties() {

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

@Nullable
private String findByClasspathBootstrapProperties() {
return findByClasspathPropertiesFile("bootstrap.properties");
}

@Nullable
Expand All @@ -153,11 +159,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 @@ -257,9 +273,16 @@ private static String parseNameFromProcessArgs(String[] args) {
}

@Nullable
private String readNameFromAppProperties() {
return loadFromClasspath(
"application.properties", SpringBootServiceNameDetector::getAppNamePropertyFromStream);
private String findByClasspathPropertiesFile(String filename) {
String result =
loadFromClasspath(filename, SpringBootServiceNameDetector::getAppNamePropertyFromStream);
if (logger.isLoggable(FINER)) {
logger.log(
FINER,
"Checking for spring.application.name in {0} file: {1}",
new Object[] {filename, result});
}
return result;
}

@Nullable
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,9 @@
spring:
application:
name: cat-store-bootstrap

mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
---
some:
other:
property: value

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