How to access a value defined in the application.properties file in Spring Boot
To access a value defined in the application.properties
file in Spring Boot, you can use the @Value
annotation and the Environment
interface.
Here's an example of how you can use the @Value
annotation to inject a property value into a field:
@Component
public class MyBean {
@Value("${my.property}")
private String myProperty;
// ...
}
In this example, the myProperty
field will be injected with the value of the my.property
property from the application.properties
file.
You can also use the Environment
interface to access property values. Here's an example of how you can use the Environment
interface to access a property value:
@Component
public class MyBean {
@Autowired
private Environment env;
public void doSomething() {
String myProperty = env.getProperty("my.property");
// ...
}
}
In this example, the doSomething()
method uses the getProperty()
method of the Environment
interface to get the value of the my.property
property.
I hope this helps! Let me know if you have any questions.