2V0-72.22PSE · Question #86
Which two ways to access the properties defined in an application.properties or application.yml file are correct? (Choose two.)
The correct answer is A. Through the getProperty() method of the Spring Environment object. B. Through a constructor parameter annotated with @Value. A and B are correct because Spring provides two idiomatic mechanisms for reading application.properties/application.yml values: the Environment object (injected as a bean) exposes a getProperty("key") method that reads from any registered PropertySource, and @Value("${key}") on…
Question
Which two ways to access the properties defined in an application.properties or application.yml file are correct? (Choose two.)
Options
- AThrough the getProperty() method of the Spring Environment object.
- BThrough a constructor parameter annotated with @Value.
- CThrough the getProperty() method of a Properties object.
- DThrough the getProperty() method of an ApplicationContext object.
- EThrough a constructor parameter annotated with @Property.
How the community answered
(54 responses)- A78% (42)
- C11% (6)
- D4% (2)
- E7% (4)
Explanation
A and B are correct because Spring provides two idiomatic mechanisms for reading application.properties/application.yml values: the Environment object (injected as a bean) exposes a getProperty("key") method that reads from any registered PropertySource, and @Value("${key}") on a constructor parameter (or field) lets Spring inject the value directly at wiring time.
Why the distractors are wrong:
- C -
java.util.Propertiesis a plain Java class unaware of the Spring context; itsgetProperty()reads from that specificPropertiesinstance, not from Spring's unified property sources. - D -
ApplicationContextdoes not expose agetProperty()method; it inherits fromEnvironmentCapableand gives you access to theEnvironmentobject, but you must then callgetProperty()on thatEnvironment, not on the context itself. - E -
@Propertydoes not exist in Spring; the real annotation is@Value. This is a classic trap using a plausible-sounding but invented name.
Memory tip: Think "Spring gives you two ports into properties - the Environment door (programmatic) and the @Value door (declarative)." If a choice says ApplicationContext.getProperty() or invents a fake annotation like @Property, it's wrong.
Topics
Community Discussion
No community discussion yet for this question.