In Spring, the default profile is a special, implicit fallback profile. If you do not explicitly activate any profile, Spring automatically considers the default profile active. As a result:
- Beans with
@Profile("default") are included.
- Beans with no
@Profile at all are always included, regardless of whether other profiles are active or not.
Example
Configuration with a default-profile bean and a no-profile bean:
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("default")
public String defaultProfileBean() {
return "Loaded from DEFAULT profile";
}
@Bean
public String noProfileBean() {
return "Loaded with NO profile";
}
@Bean
@Profile("prod")
public String prodProfileBean(){
return "Loaded with prod profile";
}
}
1) Run without activating any profile
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DefaultProfileDemo {
public static void main(String[] args) {
// No active profile explicitly set
// → Spring activates the special "default" profile
try (AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class)) {
if (context.containsBean("defaultProfileBean")) {
System.out.println(context.getBean("defaultProfileBean"));
}
if (context.containsBean("noProfileBean")) {
System.out.println(context.getBean("noProfileBean"));
}
if (context.containsBean("prodProfileBean")) {
System.out.println(context.getBean("prodProfileBean"));
}
}
}
}
OutputLoaded from DEFAULT profile Loaded with NO profile
Both beans are loaded because Spring implicitly activates the default profile when none is specified, and the bean without any @Profile is always eligible.
2) Activate another profile (e.g., prod)
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ProdProfileDemo {
public static void main(String[] args) {
// Activate a non-default profile before registering the configuration
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("prod");
context.register(AppConfig.class);
context.refresh();
// With a non-default profile active:
// - Bean with @Profile("default") is NOT loaded
// - Bean without @Profile is still loaded
if (context.containsBean("defaultProfileBean")) {
System.out.println(context.getBean("defaultProfileBean"));
}
if (context.containsBean("noProfileBean")) {
System.out.println(context.getBean("noProfileBean"));
}
if (context.containsBean("prodProfileBean")) {
System.out.println(context.getBean("prodProfileBean"));
}
context.close();
}
}
OutputLoaded with NO profile Loaded with prod profile
When a non-default profile is explicitly activated, the default profile is no longer considered active. Therefore, beans with @Profile("default") are excluded, while beans with no @Profile remain available.
Key takeaway
@Profile("default") is active only when no other profile is explicitly enabled, and it is loaded together with beans that have no profile.
Beans without @Profile are always created (unless other conditions prevent their creation).
Example ProjectDependencies and Technologies Used: - spring-context 6.2.13 (Spring Context)
Version Compatibility: 3.2.3.RELEASE - 6.2.13 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|