@Value Injection — Concept Check
Questions on @Value Injection. Injects individual property values, Spring EL expressions, or system variables directly into fields or constructor parameters.
Questions in this quiz
8 questions on @Value Injection — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to inject the application name into a Spring bean. Which annotation usage is correct?
- A@Value("${app.name}") private String appName;
- B@Value("#{app.name}") private String appName;
- C@Value("app.name") private String appName;
- D@Value("${app.name}") private int appName;
2.A service uses @Value to inject a timeout value. The property is missing in application.properties. What happens?
- AThe timeout defaults to 0.
- BThe service fails to start due to missing property.
- CThe service starts but with an undefined timeout.
- DThe timeout uses the default value specified in @Value.
3.A developer uses @Value to inject a list of admins. What is the correct format in application.properties?
- Aapp.admins={admin1,admin2}
- Bapp.admins=admin1,admin2
- Capp.admins: admin1;admin2
- Dapp.admins=[admin1,admin2]
4.In a production profile, which property file is used to override defaults?
- Aapplication-default.properties
- Bapplication-prod.properties
- Capplication-profile-prod.properties
- Dapplication.properties
5.A developer uses @Value("#{T(java.lang.Math).PI}"). What is being injected?
- AA property named PI from application.properties
- BA system property named PI
- CThe value of PI from Math class
- DA random number generated by Math.PI
6.Which is a benefit of using @ConfigurationProperties over @Value for multiple properties?
- ANo need for property files
- BType safety and validation
- CDirect injection into fields
- DSimpler syntax
7.A developer uses @Value("#{systemProperties['user.home']}"). What does this achieve?
- AInjects a random directory path
- BInjects the user's home directory path
- CInjects a property named user.home
- DInjects a default directory path
8.Why is constructor injection preferred over field injection in Spring?
- AIt is simpler to implement
- BIt improves testability
- CIt is the default Spring behavior
- DIt requires fewer annotations