Junior — Middle
How do you implement and use parameters from a Bean configuration file in a Spring application?
sobes.tech AI
Answer from AI
In Spring, to inject parameters from a configuration file into a Bean, the @Value annotation is usually used or parameters are bound through @ConfigurationProperties.
Example with @Value:
@Component
public class MyBean {
@Value("${my.property.name}")
private String propertyName;
public void printProperty() {
System.out.println(propertyName);
}
}
A value is set in application.properties or application.yml:
my.property.name=Example value
When Spring starts, it injects the value from the configuration into the propertyName field.
Alternatively, a class with @ConfigurationProperties can be used to group parameters into a single object.