-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Improve setEnvironmentPrefix(...) reference documentation #45370
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
Comments
Spring Boot 3.1.12 is out of OSS support. Please upgrade and, if you can still reproduce the issue you've described, share a small app that we can run ourselves that demonstrates what you've explained. This is a lot of text and our time would be best used looking at the actual problem to see if the documentation should be updated. Thank you. |
This code starts the application twice. First without a prefix, second with a prefix. You should remove the |
I've tried to improve the documentation. I think the first issue you describe is due to the description @mdeinum provided. If you can recreate the issue without the first static |
@mdeinum wrote:
Thanks for spotting that. That's not in the actual code. That was my stupid copy'n'paste mistake: I copied code from a pull request diff, including that original (deleted) first line. I've edited my original post and removed that line. Sorry about that. |
@snicoll wrote:
Yes, I'm aware of this. I'm not the owner of this app, nor the primary developer.
Yes, I agree with everything you've written. All of that has occurred to me, including "This is a lot of text". I'm sorry for being verbose. It's partly me overcompensating for not having that demo app to offer. I'm feeling a little like an imposter here. I'm learning Java, and Spring Boot, by examining and making small tweaks to an existing app. It would do me good to start with a new, minimal app, and work from there. I really want to do that. It's just a matter of getting the time to do it. |
@philwebb wrote:
No, it's not.
Yes, I can. As I've mentioned in a recent comment, that first call is not in my actual working code. I'm so sorry for wasting people's time with that careless copy'n'paste mistake.
Understood. |
@philwebb wrote:
Done. Here's the source of a sample Spring Boot 3.4.5 app, with detailed test cases. Could you please investigate? The sample and test cases demonstrate the issue that I reported: with an environment prefix set (in this example, to Is this a bug, or working as designed? Are other configuration properties also "insensitive" to the environment prefix? Sample Java sourceHere is my newbie attempt at a minimal demo app, created after some trial and error. I'd gratefully accept tips on how to make this more concise. I deliberately chose to use
package io.github.grahamhannington.issue45370;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Issue45370Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Issue45370Application.class);
application.setEnvironmentPrefix("nco");
application.run(args);
}
@Override
public void run(String... args) throws Exception {
while (true) {
Thread.sleep(1000);
}
}
} Test environmentOperating system: Ubuntu 24.04.1 LTS (Noble Numbat) Java version: 17 (OpenJDK Runtime Environment, build 17.0.14+7-Ubuntu-124.04) Current working directory:
Contents of server:
port: 8081 Example Java command line (as described in the test cases below, I tried various combinations of environment variable names, with and without the NCO_SPRING_CONFIG_ADDITIONALLOCATION=file:./additionallocation/ SPRING_PROFILES_ACTIVE=custom-server-port java -jar /u/myid/path/to/issue45370.jar > /u/myid/path/to/spring-boot.log Test casesTest case 1: expected failure: both variable names deliberately without environment prefixEnvironment variables: SPRING_CONFIG_ADDITIONALLOCATION=file:./additionallocation/
SPRING_PROFILES_ACTIVE=custom-server-port Log excerpt:
Interesting: I expected the app to not recognize App uses default port 8080. That is, the app does not use the server port 8081 set in Test case 2: unexpected failure: both variable names with environment prefixEnvironment variables: NCO_SPRING_CONFIG_ADDITIONALLOCATION=file:./additionallocation/
NCO_SPRING_PROFILES_ACTIVE=custom-server-port Log excerpt:
Interesting: this tells me the app does not recognize App uses default port 8080. Test case 3: unexpected success: only one variable name with environment prefixEnvironment variables: NCO_SPRING_CONFIG_ADDITIONALLOCATION=file:./additionallocation/
SPRING_PROFILES_ACTIVE=custom-server-port Log excerpt:
App uses custom server port 8081 set in |
Thanks for the reproducer @GrahamHannington. Given that we used this issue for the documentation update, I've opened a new one to deal with the bug. #45387 |
With no environment prefix set, the following environment variables work:
That is, my Spring Boot app loads the corresponding profile-specific file
../shared/nexus-server-public.yaml
, and the app uses the property values set in that file, such as theserver.port
value.Note:
--spring.config.name=nexus
.If I set the environment prefix "nco" (here's the corresponding snippet from the app
main()
function):then, as expected, those (unprefixed) environment variables no longer "work".
Interestingly, though, the app log reports that profile as being active, but the log also shows that the app does not use the server port value set by that profile-specific file.
The problem: This tells me that, with an environment prefix set, my app is using
SPRING_PROFILES_ACTIVE
, but notSPRING_CONFIG_ADDITIONALLOCATION
.Sure enough, if I add the prefix
NCO_
to the variable nameSPRING_CONFIG_ADDITIONALLOCATION
, then it works: the app uses theserver.port
set in the profile-specific file in that additional location.However, if I also add the prefix
NCO_
toSPRING_PROFILES_ACTIVE
, then the application log reports "No active profile set".That is, my app recognizes
NCO_SPRING_CONFIG_ADDITIONALLOCATION
as a configuration property, but notNCO_SPRING_PROFILES_ACTIVE
.If this is a bug in Spring Boot, then: please fix it.
If this is working as designed, then: please enhance the docs to describe, if an environment prefix is set, which configuration properties should be specfied with that prefix, and which (e.g.
SPRING_PROFILES_ACTIVE
) should not.Another possibility is that my app is causing this behavior. I've already considered that, but I can't see any reason for it, hence this issue. I acknowledge that I haven't taken the time to create a minimal app using the latest release of Spring Boot and attempt to reproduce this behavior. If Spring Boot developers respond to this issue that it's not a bug and it's not working as designed, then that will be useful to me: with apologies, I'll slink away and attempt again to figure out how the app is causing this behavior.
In any case, I suggest that the Spring Boot docs about the environment prefix could be enhanced.
In the related GitHub issue, "Add prefix support for environment variables", #3450, @philwebb wrote:
That concise design intent seems clear to me.
However, from the Spring Boot docs heading "Configuring System Environment Properties":
While I acknowledge that you "can't fix stupid" 🙂, and so this might just be my problem: those docs confuse me. Are those docs even describing the same feature as GitHub issue #3450? If so, the docs seem to me to be describing the feature "backwards". My understanding is that, if you set the prefix to
input
, then the environment variableINPUT_REMOTE_TIMEOUT
will be resolved as the propertyremote.timeout
. What does "also" mean in the phrase "will also be resolved as", and how does that relate to the phrase "rather than" in issue #3450?The text was updated successfully, but these errors were encountered: