24
24
import java .util .List ;
25
25
import java .util .Map ;
26
26
import java .util .Set ;
27
+ import java .util .concurrent .atomic .AtomicBoolean ;
27
28
import java .util .concurrent .atomic .AtomicInteger ;
28
29
import java .util .concurrent .atomic .AtomicReference ;
29
30
import java .util .function .Supplier ;
@@ -1363,18 +1364,30 @@ void shouldUseAotInitializer() {
1363
1364
@ Test
1364
1365
void fromRunsWithAdditionalSources () {
1365
1366
assertThat (ExampleAdditionalConfig .local .get ()).isNull ();
1366
- SpringApplication .from (ExampleFromMainMethod ::main ).with (ExampleAdditionalConfig .class ).run ();
1367
+ this .context = SpringApplication .from (ExampleFromMainMethod ::main )
1368
+ .with (ExampleAdditionalConfig .class )
1369
+ .run ()
1370
+ .getApplicationContext ();
1367
1371
assertThat (ExampleAdditionalConfig .local .get ()).isNotNull ();
1368
1372
ExampleAdditionalConfig .local .set (null );
1369
1373
}
1370
1374
1371
1375
@ Test
1372
1376
void fromReturnsApplicationContext () {
1373
- ConfigurableApplicationContext context = SpringApplication .from (ExampleFromMainMethod ::main )
1377
+ this . context = SpringApplication .from (ExampleFromMainMethod ::main )
1374
1378
.with (ExampleAdditionalConfig .class )
1375
1379
.run ()
1376
1380
.getApplicationContext ();
1377
- assertThat (context ).isNotNull ();
1381
+ assertThat (this .context ).isNotNull ();
1382
+ }
1383
+
1384
+ @ Test
1385
+ void fromWithMultipleApplicationsOnlyAppliesAdditionalSourcesOnce () {
1386
+ this .context = SpringApplication .from (MultipleApplicationsMainMethod ::main )
1387
+ .with (SingleUseAdditionalConfig .class )
1388
+ .run ()
1389
+ .getApplicationContext ();
1390
+ assertThatNoException ().isThrownBy (() -> this .context .getBean (SingleUseAdditionalConfig .class ));
1378
1391
}
1379
1392
1380
1393
private <S extends AvailabilityState > ArgumentMatcher <ApplicationEvent > isAvailabilityChangeEventWithState (
@@ -1949,6 +1962,31 @@ static void main(String[] args) {
1949
1962
1950
1963
}
1951
1964
1965
+ static class MultipleApplicationsMainMethod {
1966
+
1967
+ static void main (String [] args ) {
1968
+ SpringApplication application = new SpringApplication (ExampleConfig .class );
1969
+ application .setWebApplicationType (WebApplicationType .NONE );
1970
+ application .addListeners (new ApplicationListener <ApplicationEnvironmentPreparedEvent >() {
1971
+
1972
+ @ Override
1973
+ public void onApplicationEvent (ApplicationEnvironmentPreparedEvent event ) {
1974
+ SpringApplicationBuilder builder = new SpringApplicationBuilder (
1975
+ InnerApplicationConfiguration .class );
1976
+ builder .web (WebApplicationType .NONE );
1977
+ builder .run ().close ();
1978
+ }
1979
+
1980
+ });
1981
+ application .run (args );
1982
+ }
1983
+
1984
+ static class InnerApplicationConfiguration {
1985
+
1986
+ }
1987
+
1988
+ }
1989
+
1952
1990
@ Configuration
1953
1991
static class ExampleAdditionalConfig {
1954
1992
@@ -1960,4 +1998,17 @@ static class ExampleAdditionalConfig {
1960
1998
1961
1999
}
1962
2000
2001
+ @ Configuration
2002
+ static class SingleUseAdditionalConfig {
2003
+
2004
+ private static AtomicBoolean used = new AtomicBoolean (false );
2005
+
2006
+ SingleUseAdditionalConfig () {
2007
+ if (!used .compareAndSet (false , true )) {
2008
+ throw new IllegalStateException ("Single-use configuration has already been used" );
2009
+ }
2010
+ }
2011
+
2012
+ }
2013
+
1963
2014
}
0 commit comments