Spring / Spring7 basics Interview questions
What is the purpose of the @Value annotation?
@Value injects a single value - typically read from application properties, environment variables, or a PropertySource - directly into a field, constructor parameter, or setter, using ${...} placeholder syntax. It also supports Spring Expression Language (SpEL) for simple computed defaults.
@Component public class MailConfig { @Value("${mail.retry.count:3}") private int retryCount; }
In the example above, if mail.retry.count isn't defined in application.properties, Spring falls back to the default value 3 after the colon. @Value is convenient for individual settings, but when a whole group of related properties needs binding, @ConfigurationProperties on a dedicated class is the more maintainable choice.
More Related questions...