1

I have a situation where I need a property value (application.name) before the application context is loaded - spring boot, before SpringApplication.run is called, and during context loading.

In my ApplicationMain class which calls SpringApplication.run, I have a constant public static final NAME = "MyApplicationName".

My issue is that we have some libraries that are injecting the value via @Value("${application.name}").

Is there a way set the application.name property in the context from this constant value?

I was thinking I could do a post-construct and then set it in the environment - similar to the solution described here, but that doesn't guarantee that it will be available for all injections by @Value.

I was hoping there was a similar mechanism to @Bean on a method which could set the value for the property, but I don't see one anywhere.

divibisan
  • 11,659
  • 11
  • 40
  • 58
Townsfolk
  • 1,282
  • 1
  • 9
  • 21

1 Answers1

1

Variables can be injected on Spring Boot in several ways, but for me one of the best way is using operating system variables in *nix or Windows you can define variables like this:

Windows
    SET APPLICATION_NAME = "MY APP"

*nix
    export APPLICATION_NAME = "MY APP"

Be warned that the underline character (_) is replaced by . inside your application. Other way is using Java properties defined using -Dapplication.name="MY APP", that goes in your application command line. After you defined that variables in the operating system you can catch them in your code using:

    @Value("${application.name}")

So, again, I recommend you using environmental variables to store your application configuration, so the operators or devops people will be able to change your application behavior without touch code or internal properties files (inside your application package).

Also another way is setting System properties (System.setProperty), that will make Java applications receive variables set in run-time. Actually is the same map (System map) used using -D in command line, however it is done in run-time instead of in command line. An example:

      System.setProperty("application.name","MY APP");
rod.dinis
  • 1,233
  • 2
  • 11
  • 20
  • Yeah, we use ENV variables for everything that is changeable. This is not one of those cases. The application name is specific and unique to the application and should not be changed by operations or devops. To help uniquely identify instances of an application we'll we use an ENV variable like `INSTANCE_NO`. – Townsfolk May 24 '18 at 22:30
  • 1
    I think I understand what are your requirement now, that is kind of dynamically change variables for spring boot, very specific requirement... I was wondering if you could do that creating environmental variables on operating system inside your code... But that is just a shot. – rod.dinis May 24 '18 at 22:41
  • I don't think ENV variables can be set from within Java (at least not easily: https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java), but you can set System properties (System.setProperty) and those are used during context load. If you update your answer I'll mark it. Thanks! – Townsfolk May 24 '18 at 22:57