Cheatsheet
Properties, profiles and SpEL
Key idea
Configuration that changes between environments lives outside the code. Spring's `Environment` combines several property sources (files, environment variables, system properties) and knows which profiles are active; `@Value` injects their values and `@Profile` decides which beans exist.
Diagram (UML)
flowchart LR
subgraph sources["Property sources (first match wins)"]
direction TB
sys["JVM system properties<br/>-Dbox.name=…"]
envv["OS environment variables"]
file["@PropertySource<br/>box.properties"]
sys --> envv --> file
end
sources --> env["«Environment»<br/>getProperty · getRequiredProperty<br/>active profiles"]
env -- "${box.name} / ${box.capacity:10}" --> settings["BoxSettings<br/>«@Value»"]
roster["coachRoster «bean»"] -- "#{coachRoster.size() * 6}" --> settings
pspc["PropertySourcesPlaceholderConfigurer<br/>«static @Bean»"] -. "strict ${…}" .-> settings
env -- "profile prod" --> smtp["SmtpMailer «@Profile prod»"]
env -- "no profile → default" --> console["ConsoleMailer «@Profile dev, default»"]Key pieces
@PropertySource("classpath:box.properties"): adds a file to theEnvironmentEnvironment:getProperty(key),getProperty(key, default),getProperty(key, Type),getRequiredProperty(key),getActiveProfiles()@Value("${key:default}"): property placeholder;@Value("#{expression}"): SpELPropertySourcesPlaceholderConfigurer: resolves${…}strictly; declared with a static@Bean@Profile("prod"),@Profile({"dev", "default"}),@Profile("!prod"),@Profile("dev & cloud")- Activating profiles:
spring.profiles.active,env.setActiveProfiles(),@ActiveProfilesin tests
BoxSettings.java
public BoxSettings(@Value("${box.name}") String name, // ${…}: a property placeholder
@Value("${box.capacity:10}") int capacity, // ":10" is the default if missing
@Value("#{'${box.plans}'.split(',')}") List<String> plans, // split "a,b,c" with SpEL
@Value("#{${box.capacity} * 2}") int waitlist, // #{…}: SpEL, placeholder inside
@Value("#{coachRoster.size() * 6}") int classesPerDay, // a bean, by name
@Value("#{systemProperties['box.tz'] ?: 'Europe/Madrid'}") String timezone) { // Elvis
// …
}Remember for the exam
${…}reads a property;#{…}evaluates a SpEL expression; they can be nested:#{${box.capacity} * 2}- In SpEL a bean is referenced by name:
#{coachRoster.size()}; a class withT(java.lang.Math) - Handy SpEL variables:
systemProperties,systemEnvironment; Elvis operator?:and safe navigation?. - In plain Spring, system properties (
-D) beat environment variables, which beat@PropertySource - When no profile is active, the
defaultprofile is activated - Profiles are activated before refreshing the context; changing them afterwards has no effect
Exam traps
- Without
PropertySourcesPlaceholderConfigurer, a missing${key}stays as literal text; with it (and in Spring Boot), startup fails - The
PropertySourcesPlaceholderConfigurer@Beanmust bestatic; otherwise it's created late and that class's@Values aren't resolved - In plain Spring,
@Value("${list}") List<String>doesn't split on commas (you get one element); useString[], SpEL.split(',')or Spring Boot getPropertyreturnsnullfor a missing key;getRequiredPropertythrowsIllegalStateException- A bean without
@Profilealways exists, whatever profile is active