get value from Spring application.properties
To retrieve values from the Spring application.properties file, you can follow these steps:
Create an application.properties file: In your Spring project, create a file named "application.properties" in the src/main/resources directory. This file will be used to store configuration properties for your application.
Define properties in the file: Inside the application.properties file, you can define key-value pairs for your configuration properties. For example, you can define a property like "app.name=My Application" to set the value of "app.name" to "My Application".
Use @Value annotation: In your Spring component or bean class, you can use the @Value annotation to inject the values from the application.properties file. For example, if you have a property defined as "app.name=My Application", you can inject it into a field in your class using the following code:
java
@Value("${app.name}")
private String applicationName;
In this code, the "${app.name}" expression is used to reference the value of the "app.name" property defined in the application.properties file. The value will be injected into the "applicationName" field.
- Access the property value: Once you have injected the property value into a field using the @Value annotation, you can access it in your code. For example, you can use the "applicationName" field like this:
java
System.out.println("Application Name: " + applicationName);
This will print the value of the "app.name" property from the application.properties file.
By following these steps, you can easily retrieve values from the Spring application.properties file using the @Value annotation.