Skip to content

Commit 4877e4d

Browse files
committed
Allow the configuration of active profiles in SpringApplication.Augmented
Closes gh-36660
1 parent eff7613 commit 4877e4d

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ public static int exit(ApplicationContext context, ExitCodeGenerator... exitCode
14301430
*/
14311431
public static SpringApplication.Augmented from(ThrowingConsumer<String[]> main) {
14321432
Assert.notNull(main, "Main must not be null");
1433-
return new Augmented(main, Collections.emptySet());
1433+
return new Augmented(main, Collections.emptySet(), Collections.emptySet());
14341434
}
14351435

14361436
/**
@@ -1492,9 +1492,12 @@ public static class Augmented {
14921492

14931493
private final Set<Class<?>> sources;
14941494

1495-
Augmented(ThrowingConsumer<String[]> main, Set<Class<?>> sources) {
1495+
private final Set<String> additionalProfiles;
1496+
1497+
Augmented(ThrowingConsumer<String[]> main, Set<Class<?>> sources, Set<String> additionalProfiles) {
14961498
this.main = main;
14971499
this.sources = Set.copyOf(sources);
1500+
this.additionalProfiles = additionalProfiles;
14981501
}
14991502

15001503
/**
@@ -1506,7 +1509,20 @@ public static class Augmented {
15061509
public Augmented with(Class<?>... sources) {
15071510
LinkedHashSet<Class<?>> merged = new LinkedHashSet<>(this.sources);
15081511
merged.addAll(Arrays.asList(sources));
1509-
return new Augmented(this.main, merged);
1512+
return new Augmented(this.main, merged, this.additionalProfiles);
1513+
}
1514+
1515+
/**
1516+
* Return a new {@link SpringApplication.Augmented} instance with additional
1517+
* profiles that should be applied when the application runs.
1518+
* @param profiles the profiles that should be applied
1519+
* @return a new {@link SpringApplication.Augmented} instance
1520+
* @since 3.4.0
1521+
*/
1522+
public Augmented withAdditionalProfiles(String... profiles) {
1523+
Set<String> merged = new LinkedHashSet<>(this.additionalProfiles);
1524+
merged.addAll(Arrays.asList(profiles));
1525+
return new Augmented(this.main, this.sources, merged);
15101526
}
15111527

15121528
/**
@@ -1518,6 +1534,7 @@ public SpringApplication.Running run(String... args) {
15181534
RunListener runListener = new RunListener();
15191535
SpringApplicationHook hook = new SingleUseSpringApplicationHook((springApplication) -> {
15201536
springApplication.addPrimarySources(this.sources);
1537+
springApplication.setAdditionalProfiles(this.additionalProfiles.toArray(String[]::new));
15211538
return runListener;
15221539
});
15231540
withHook(hook, () -> this.main.accept(args));

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import org.springframework.context.annotation.Bean;
9393
import org.springframework.context.annotation.Configuration;
9494
import org.springframework.context.annotation.Lazy;
95+
import org.springframework.context.annotation.Profile;
9596
import org.springframework.context.event.ApplicationEventMulticaster;
9697
import org.springframework.context.event.ContextRefreshedEvent;
9798
import org.springframework.context.event.SimpleApplicationEventMulticaster;
@@ -1409,7 +1410,8 @@ public void contextLoaded(ConfigurableApplicationContext context) {
14091410
then(listener).should(never()).onApplicationEvent(any(ApplicationFailedEvent.class));
14101411
}
14111412

1412-
@Test // gh-32555
1413+
@Test
1414+
// gh-32555
14131415
void shouldUseAotInitializer() {
14141416
SpringApplication application = new SpringApplication(ExampleAotProcessedMainClass.class);
14151417
application.setWebApplicationType(WebApplicationType.NONE);
@@ -1468,6 +1470,17 @@ void fromWithMultipleApplicationsOnlyAppliesAdditionalSourcesOnce() {
14681470
assertThatNoException().isThrownBy(() -> this.context.getBean(SingleUseAdditionalConfig.class));
14691471
}
14701472

1473+
@Test
1474+
void fromAppliesProfiles() {
1475+
this.context = SpringApplication.from(ExampleFromMainMethod::main)
1476+
.with(ProfileConfig.class)
1477+
.withAdditionalProfiles("custom")
1478+
.run()
1479+
.getApplicationContext();
1480+
assertThat(this.context).isNotNull();
1481+
assertThat(this.context.getBeanProvider(Example.class).getIfAvailable()).isNotNull();
1482+
}
1483+
14711484
@Test
14721485
void shouldStartDaemonThreadIfKeepAliveIsEnabled() {
14731486
SpringApplication application = new SpringApplication(ExampleConfig.class);
@@ -2159,4 +2172,15 @@ static class SingleUseAdditionalConfig {
21592172

21602173
}
21612174

2175+
@Configuration
2176+
static class ProfileConfig {
2177+
2178+
@Bean
2179+
@Profile("custom")
2180+
Example example() {
2181+
return new Example();
2182+
}
2183+
2184+
}
2185+
21622186
}

0 commit comments

Comments
 (0)