6

I am trying to get the value of a property

hello.world=Hello World

in MainApp class

@SpringBootApplication
public class MainApp {
   public static void main(String[] args) {
      SpringApplication.run(MainApp.class, args);
}

This didn't work as its the main method.

@Value("${hello.world}")
public static String helloWorld;

Maybe its possible to load by

  Properties prop = new Properties();

    // load a properties file
    prop.load(new FileInputStream(filePath));

Is there any other better way to get the properties using Spring in the main method of SpringBoot before SpringApplication.run

Cork Kochi
  • 1,783
  • 6
  • 29
  • 45

6 Answers6

8
ConfigurableApplicationContext ctx = 
           SpringApplication.run(MainApp.class, args);
String str = ctx.getEnvironment().getProperty("some.prop");
System.out.println("=>>>> " + str);
sanit
  • 1,646
  • 1
  • 18
  • 21
  • After this again we have to call app.run(args) to start the application? – Cork Kochi Jan 08 '18 at 18:41
  • We usually call `run()` method to start spring boot application. `run()` returns `ConfigurableApplicationContext` which in turn can return `ConfigurableEnvironment` that contain method to pull properties. To verify see your `SpringApplication.run(MainApp.class, args)` statement, it will also be returning `ConfigurableApplicationContext`. – sanit Jan 08 '18 at 18:44
  • What if we I have to get before SpringApplication.run ? – Cork Kochi Jan 08 '18 at 19:00
  • 2
    When we call `SpringApplication.run()` then it starts tasks like auto-wiring dependency etc. Before that with the help of spring we can't fetch any property, to make this happen you have to read properties file manually. Can you please specify what is the use-case that you want to read property before `run()` invoking method? – sanit Jan 08 '18 at 19:07
  • Then I believe you have to follow manual process that you have mentioned earlier to load properties file and read it. – sanit Jan 08 '18 at 19:14
3

You have declared the variable helloWorld as static. Hence you need to use Setter Injection and not Field Injection.

Injecting a static non-final field is a bad practice. Hence Spring doesn't allow it. But you can do a workaround like this.

public static String helloWorld;

      @Value("${hello.world}")
        public void setHelloWorld(String someStr) {
          helloWorld = someStr
        }

You can access this variable helloWorld at any point in the class, if its any other class. But if you want to do it in the main class. You can access the variable only after this line

SpringApplication.run(MainApp.class, args);)

i.e only after the application has started.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • 2
    of course it works. Only thing is we can access the variable only after `SpringApplication.run(....)` – pvpkiran Jan 08 '18 at 19:06
2

Don't do this. Its better to use CommandLineRunner. Thanks to this you can have a non static method that Spring Boot will run for you automatically:

@SpringBootApplication
public class SimulatorApplication implements CommandLineRunner {

@Value("${my-value}")
private myValue;

public static void main(String[] args) {
  SpringApplication.run(SimulatorApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
  // here you can access my-value
}

}

Tomasz S
  • 192
  • 2
  • 2
  • 9
  • 1
    To have Spring Boot get property my-value from application.properties use @Value(${my-value}) myValue Not, as you have it @Value("myvalue") myValue which means String myValue = "my-value"; – Net Dawg Oct 05 '21 at 02:31
  • Thank you @NetDawg, fixed in above answer. – Tomasz S Dec 13 '21 at 08:09
1
@SpringBootApplication
public class MainApp {
   public static void main(String[] args) {
     SpringApplication springApplication = new SpringApplication(MainApp.class);
     springApplication.addListeners(new VersionLogger());
     springApplication.run(args);
}


// The VersionLogger Class
public class VersionLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent>{

  @Override
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEvent) {
    String helloWorld = applicationEvent.getEnvironment().getProperty("hello.world");
  }
}

ApplicationEnvironmentPreparedEvent Event published when a SpringApplication is starting up and the Environment is first available for inspection and modification.

JosephH
  • 37,173
  • 19
  • 130
  • 154
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
1
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);

String applicationPropertyVersion=applicationContext.getEnvironment().getProperty("application.property.version");

    LOGGER.info("RELEASE CODE VERSION {} and applicationProperty Version {} ", LcoBuildVersion.version,
            applicationPropertyVersion);
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    Hi Vishal. Thanks for your answer. Usually it's useful to provide a few words to explain the solution. – MaxV Oct 16 '20 at 18:45
0

We can't read values into static fields . Here is the explanation gives a better insight How to assign a value from application.properties to a static variable?

Amaravathi
  • 115
  • 1
  • 2
  • 6