So I have an application.yml
file for my spring boot application like so:
spring:
url: localhost
email:
from: something@gmail.com
app:
uuid: 3848348j34jk2dne9
I want to wire these configuration properties into different components in my app like so:
@Component
public class FooA {
private final String url;
public FooA(@Value("${spring.url}") String url) {
this.url = url
}
}
@Component
public class FooB {
private final String from;
public FooA(@Value("${email.from}") String from) {
this.from = from
}
}
@Component
public class FooC {
private final String uuid;
public FooA(@Value("${app.uuid}") String uuid) {
this.uuid = uuid
}
}
The above works as intended in my application. But my question is if this is best practice in spring boot. The only other alternative to this that I know of is to use a Properties
object by creating a bean inside of a configuration class, loading the properties with all the configuration variables and autowire the property bean into the components.
What is the best practice in this case?