Sub topic 4
Reading single values with @Value and Environment
This chapter assumes you can run a Spring Boot application and know constructor injection. It also assumes you have seen the app.mail.* properties from application.yaml. Two tools read one property at a time: the @Value annotation and the Environment interface. Both read from the same merged set of property sources.
Injecting a value with @Value
@Value takes a placeholder written as ${key}. Spring resolves it when it creates the bean and converts the text to the parameter type.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MailSettingsReporter {
private final String host;
private final int port;
private final int retryAttempts;
public MailSettingsReporter(
@Value("${app.mail.host}") String host,
@Value("${app.mail.port}") int port,
@Value("${app.mail.retry-attempts:3}") int retryAttempts) {
this.host = host;
this.port = port;
this.retryAttempts = retryAttempts;
}
public String describe() {
return host + ":" + port + " (retries: " + retryAttempts + ")";
}
}The text after the colon in ${app.mail.retry-attempts:3} is a default. Spring uses it when no source defines the key. Without a default, a missing key stops startup with Could not resolve placeholder 'app.mail.host'.
Reading on demand with Environment
Environment is a bean you can inject. It lets you look up a key at the moment you need it, and it lets the key be computed.
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentReporter {
private final Environment env;
public EnvironmentReporter(Environment env) {
this.env = env;
}
public String describe() {
String host = env.getRequiredProperty("app.mail.host");
Integer port = env.getProperty("app.mail.port", Integer.class, 25);
return host + ":" + port;
}
}getProperty returns null, or the default you pass, when the key is missing. getRequiredProperty throws IllegalStateException instead. Environment also tells you which profiles are active through getActiveProfiles().
Choosing between them
| Need | Use |
|---|---|
| A fixed value required to build a bean | `@Value` in the constructor |
| A key built at runtime, or an optional lookup | `Environment` |
| Several related values | `@ConfigurationProperties`, covered later |
Common errors
- Writing the key in camel case. With
@Value("${app.mail.retryAttempts}")the key must match what the sources define. Use the lowercase kebab-case form, as inapp.mail.retry-attempts. Then environment variables such asAPP_MAIL_RETRYATTEMPTSstill resolve. - Using
@Valueoutside a bean. Spring only processes it on beans it creates. It ignoresstaticfields. - Skipping defaults on optional values. A missing key fails startup unless the placeholder has a default.
- Confusing
$with#.${...}reads a property.#{...}is a SpEL expression, a separate feature.
Exercise
A bean has @Value("${app.mail.sender}") String sender, and no source defines app.mail.sender. What happens at startup, and what is the smallest change that lets the application start?
Show answer
Startup fails with an exception saying it could not resolve the placeholder app.mail.sender. Adding a default, such as ${app.mail.sender:[email protected]}, lets the application start.