← Learning Hub
Intermediate

Configuration, Profiles and Secrets

Settings go in src/main/resources/application.properties (or application.yml), not in your code.

The same app runs in development, testing and production with different databases, URLs and secrets. Spring's configuration system - properties files, profiles and environment variables - keeps those differences out of your code.

Example
# application.properties
spring.application.name=shop
server.port=8080
shop.gst-percent=18
shop.free-delivery-above=499
shop.support-email=support@example.com

Reading settings: @ConfigurationProperties

Example
@ConfigurationProperties(prefix = "shop")
public record ShopSettings(int gstPercent, int freeDeliveryAbove, String supportEmail) {}

@SpringBootApplication
@ConfigurationPropertiesScan          // finds ShopSettings
public class ShopApplication { ... }

@Service
public class DeliveryService {
    private final ShopSettings settings;
    public DeliveryService(ShopSettings settings) { this.settings = settings; }

    public int deliveryFee(int total) {
        return total >= settings.freeDeliveryAbove() ? 0 : 40;
    }
}

Spring matches gst-percent in the file to gstPercent in Java automatically. For a single value you can also use @Value("${shop.gst-percent}") int gst.

Profiles: dev vs prod

Example
# application-dev.properties
spring.datasource.url=jdbc:h2:mem:shop
logging.level.com.terixto=DEBUG

# application-prod.properties
spring.datasource.url=jdbc:postgresql://db:5432/shop
spring.datasource.username=shop
spring.datasource.password=${DB_PASSWORD}     # read from an environment variable
Example
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
java -jar shop.jar --spring.profiles.active=prod
# or set the environment variable SPRING_PROFILES_ACTIVE=prod

Environment variables win

Any setting can be overridden by an environment variable: SERVER_PORT=9090 overrides server.port, SHOP_GST_PERCENT=12 overrides shop.gst-percent. This is how servers and Docker pass in passwords and keys.

Never commit real passwords or API keys in properties files. Use ${ENV_NAME} placeholders and set the real values on the server.
Key points
  • Settings live in application.properties.
  • Profiles switch settings per environment.
  • Environment variables override settings; keep secrets there.

📝 Test yourself

1. Where do Spring Boot settings usually live?
2. Which environment variable overrides server.port?
3. Fill in the setting that chooses a profile:
--spring.profiles.=prod