Profiles is a functional interface (since Spring 5.1) which is used to evaluate whether a given set of active profiles matches a profile expression. It is essentially the programmatic engine behind annotations like @Profile and methods like Environment#acceptsProfiles(...).
Source
Definition of ProfilesVersion: 6.2.13 package org.springframework.core.env;
@FunctionalInterface
public interface Profiles {
boolean matches(Predicate<String> isProfileActive); 1
static Profiles of(String... profileExpressions); 2
}
Following expressions are supported when creating Profiles instance:
! (A logical NOT of the profile name or compound expression)
& (A logical AND of the profile names or compound expressions)
| (A logical OR of the profile names or compound expressions)
Examples
Configuration
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.logicbig.example")
public class AppConfig {
}
1) Using Profiles
package com.logicbig.example;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class ProfileChecker {
private final Environment env;
public ProfileChecker(Environment env) {
this.env = env;
}
public void checkProfiles() {
System.out.println("Active profiles: " +
Arrays.toString(env.getActiveProfiles()));
// Create a Profiles object for the "dev" OR "staging" environment
Profiles devOrStaging = Profiles.of("dev|staging");
if (env.acceptsProfiles(devOrStaging)) {
System.out.println("Running in a non-production environment "
+ "(dev or staging).");
} else {
System.out.println("Not running in dev or staging.");
}
// Example of a more complex expression: "dev & !cloud"
Profiles devAndNotCloud = Profiles.of("dev & !cloud");
System.out.println("dev & !cloud matched? " +
env.acceptsProfiles(devAndNotCloud));
}
}
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ProfilesMain {
public static void main(String[] args) {
// Activate a typical development setup
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev");
ctx.register(AppConfig.class);
ctx.refresh();
try {
ProfileChecker checker = ctx.getBean(ProfileChecker.class);
checker.checkProfiles();
} finally {
ctx.close();
}
}
}
OutputActive profiles: [dev] Running in a non-production environment (dev or staging). dev & !cloud matched? true
2) Using Profiles in Java 8 Streams
package com.logicbig.example;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class StreamProfileService {
private final Environment env;
public StreamProfileService(Environment env) {
this.env = env;
}
public void processEnvironmentSpecificTasks() {
List<String> requiredConfigs = Arrays.asList(
"aws",
"mongodb",
"redis",
"local-db",
// demonstrate expression usage within stream
"dev & !cloud"
);
List<String> activeConfigs =
requiredConfigs.stream()
// Profiles.of returns the Profiles functional interface
// Environment.acceptsProfiles consumes it
.filter(expr -> env.acceptsProfiles(Profiles.of(expr)))
.collect(Collectors.toList());
System.out.println("Active profiles (stream check): "
+ Arrays.toString(env.getActiveProfiles()));
System.out.println("Active configurations for this environment: " +
activeConfigs);
}
}
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ProfilesStreamsMain {
public static void main(String[] args) {
// Simulate an environment with multiple active profiles
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev", "redis");
ctx.register(AppConfig.class);
ctx.refresh();
try {
StreamProfileService svc =
ctx.getBean(StreamProfileService.class);
svc.processEnvironmentSpecificTasks();
} finally {
ctx.close();
}
}
}
OutputActive profiles (stream check): [dev, redis] Active configurations for this environment: [redis, dev & !cloud]
Key takeaways
- Use
Profiles.of(...) to build a matcher from simple names or logical expressions.
- Call
Environment.acceptsProfiles(Profiles) to evaluate against the current active profiles.
- Prefer this over manual string checks of
Environment#getActiveProfiles() to leverage Spring's expression parsing and stay consistent with @Profile semantics.
- Because
Profiles is a functional interface, it fits naturally into APIs that accept lambdas and can be used with streams for concise filtering.
Example ProjectDependencies and Technologies Used: - spring-context 6.2.13 (Spring Context)
Version Compatibility: 5.1.0.RELEASE - 6.2.13 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|