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)

ApplicationContextcoachRosterbox.propertiesEnvironmentBoxSettings-Dbox.name=…WelcomeServiceConsoleMailer (dev, default)SmtpMailer (prod)

Key pieces

  • @PropertySource("classpath:box.properties"): adds a file to the Environment
  • Environment: getProperty(key), getProperty(key, default), getProperty(key, Type), getRequiredProperty(key), getActiveProfiles()
  • @Value("${key:default}"): property placeholder; @Value("#{expression}"): SpEL
  • PropertySourcesPlaceholderConfigurer: resolves ${…} strictly; declared with a static @Bean
  • @Profile("prod"), @Profile({"dev", "default"}), @Profile("!prod"), @Profile("dev & cloud")
  • Activating profiles: spring.profiles.active, env.setActiveProfiles(), @ActiveProfiles in 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 with T(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 default profile 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 @Bean must be static; 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); use String[], SpEL .split(',') or Spring Boot
  • getProperty returns null for a missing key; getRequiredProperty throws IllegalStateException
  • A bean without @Profile always exists, whatever profile is active

← Properties, profiles and SpEL · Run it