get value from Spring application.properties

https:/‮www/‬.lautturi.com
get value from Spring application.properties

In a Spring application, you can use the @Value annotation to inject a value from the application.properties file into a field or a method parameter. Here's an example of how you could do this:

import org.springframework.beans.factory.annotation.Value;

public class MyBean {
  @Value("${my.property}")
  private String myProperty;

  public void printProperty() {
    System.out.println("My property: " + myProperty);
  }
}

In this example, the myProperty field will be injected with the value of the my.property property from the application.properties file.

To use the @Value annotation, you will need to enable the spring-context-support module in your project. You can do this by adding the following dependency to your pom.xml file (if you are using Maven) or by adding the equivalent dependency to your build file (if you are using a different build tool):

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>5.3.5</version>
</dependency>

Note that the @Value annotation can also be used to inject values from other sources, such as system properties or environment variables.

Created Time:2017-11-01 12:04:59  Author:lautturi