Hackforge Academy

Category: java

What are Spring Profiles?

Published on 24 Jul 2026

Explanation

Spring Profiles allow you to maintain different configurations for different environments such as development, testing, and production. By activating a specific profile, Spring Boot loads only the configuration properties associated with that environment.

Code:

# application.properties
spring.profiles.active=dev

Explanation

Spring Boot supports separate property files for each environment. For example, application-dev.properties and application-prod.properties can contain different database URLs, logging levels, and server configurations without changing the application code.

Code:

application.properties
application-dev.properties
application-test.properties
application-prod.properties

Explanation

The @ConfigurationProperties annotation maps multiple configuration values from property files into a Java class. It provides type safety, better organization, and is preferred over multiple @Value annotations for related properties.

Code:

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private String version;

    // Getters and Setters
}

Explanation

Use the @Value annotation to inject individual property values directly into Spring-managed beans. It is suitable for reading a small number of configuration values from application.properties or environment variables.

Code:

@Value("${server.port}")
private int serverPort;

@Value("${spring.application.name}")
private String appName;

Explanation

Externalized Configuration allows application settings to come from environment variables, command-line arguments, Docker containers, or cloud platforms. This makes applications portable and eliminates the need to rebuild the application for configuration changes.

Code:

# Command Line
java -jar app.jar --server.port=9090

# Environment Variable
export SERVER_PORT=9090

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community