Spring / Spring Boot 4 Basics Interview Questions
What is Spring Boot's externalized configuration with @Value and @ConfigurationProperties?
Spring Boot 4 provides two ways to inject configuration values into beans: @Value for simple individual values and @ConfigurationProperties for structured, type-safe groups of related properties.
# application.yml: app: payment: gateway-url: https://payment.example.com/api timeout-seconds: 30 retry-attempts: 3 supported-currencies: [USD, EUR, GBP, JPY] api: rate-limit: 1000 // Method 1: @Value for simple individual values @Service public class RateLimiter { @Value("${api.rate-limit}") private int rateLimit; @Value("${api.rate-limit:500}") // default value if not configured private int rateLimitWithDefault; @Value("${MISSING_PROP:#{null}}") // null default private String optionalProp; @Value("${spring.application.name}") private String appName; } // Method 2: @ConfigurationProperties for grouped values (recommended) @ConfigurationProperties(prefix = "app.payment") @Validated public record PaymentProperties( @NotBlank URL gatewayUrl, @Positive int timeoutSeconds, @Min(1) @Max(10) int retryAttempts, @NotEmpty List<String> supportedCurrencies ) {} // Register and inject: @EnableConfigurationProperties(PaymentProperties.class) @SpringBootApplication public class App { ... } @Service @RequiredArgsConstructor public class PaymentService { private final PaymentProperties config; // type-safe injection public void processPayment(String currency) { if (!config.supportedCurrencies().contains(currency)) { throw new UnsupportedCurrencyException(currency); } } }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
