Skip to content

Commit 0c00508

Browse files
committed
Register ApplicationConversionService for context
Update `SpringApplication` to automatically register the shared `ApplicationConversionService` instance with the `BeanFactory` and `Environment`. Closes gh-12148
1 parent ab6bdc7 commit 0c00508

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.boot.context.properties.bind.Bindable;
4646
import org.springframework.boot.context.properties.bind.Binder;
4747
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
48+
import org.springframework.boot.convert.ApplicationConversionService;
4849
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
4950
import org.springframework.context.ApplicationContext;
5051
import org.springframework.context.ApplicationContextInitializer;
@@ -214,6 +215,8 @@ public class SpringApplication {
214215

215216
private boolean addCommandLineProperties = true;
216217

218+
private boolean addConversionService = true;
219+
217220
private Banner banner;
218221

219222
private ResourceLoader resourceLoader;
@@ -397,7 +400,6 @@ private void prepareContext(ConfigurableApplicationContext context,
397400
logStartupInfo(context.getParent() == null);
398401
logStartupProfileInfo(context);
399402
}
400-
401403
// Add boot specific singleton beans
402404
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
403405
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
@@ -503,6 +505,10 @@ private ConfigurableEnvironment getOrCreateEnvironment() {
503505
*/
504506
protected void configureEnvironment(ConfigurableEnvironment environment,
505507
String[] args) {
508+
if (this.addConversionService) {
509+
environment.setConversionService(
510+
ApplicationConversionService.getSharedInstance());
511+
}
506512
configurePropertySources(environment, args);
507513
configureProfiles(environment, args);
508514
}
@@ -644,6 +650,10 @@ protected void postProcessApplicationContext(ConfigurableApplicationContext cont
644650
.setClassLoader(this.resourceLoader.getClassLoader());
645651
}
646652
}
653+
if (this.addConversionService) {
654+
context.getBeanFactory().setConversionService(
655+
ApplicationConversionService.getSharedInstance());
656+
}
647657
}
648658

649659
/**
@@ -1046,6 +1056,16 @@ public void setAddCommandLineProperties(boolean addCommandLineProperties) {
10461056
this.addCommandLineProperties = addCommandLineProperties;
10471057
}
10481058

1059+
/**
1060+
* Sets if the {@link ApplicationConversionService} should be added to the application
1061+
* context's {@link Environment}.
1062+
* @param addConversionService if the application conversion service should be added
1063+
* @since 2.1.0
1064+
*/
1065+
public void setAddConversionService(boolean addConversionService) {
1066+
this.addConversionService = addConversionService;
1067+
}
1068+
10491069
/**
10501070
* Set default environment properties which will be used in addition to those in the
10511071
* existing {@link Environment}.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import org.springframework.boot.Banner;
3232
import org.springframework.boot.SpringApplication;
3333
import org.springframework.boot.WebApplicationType;
34+
import org.springframework.boot.convert.ApplicationConversionService;
3435
import org.springframework.context.ApplicationContext;
3536
import org.springframework.context.ApplicationContextInitializer;
3637
import org.springframework.context.ApplicationListener;
3738
import org.springframework.context.ConfigurableApplicationContext;
3839
import org.springframework.core.env.ConfigurableEnvironment;
40+
import org.springframework.core.env.Environment;
3941
import org.springframework.core.io.ResourceLoader;
4042
import org.springframework.util.StringUtils;
4143

@@ -370,6 +372,19 @@ public SpringApplicationBuilder addCommandLineProperties(
370372
return this;
371373
}
372374

375+
/**
376+
* Flag to indicate if the {@link ApplicationConversionService} should be added to the
377+
* application context's {@link Environment}.
378+
* @param addConversionService if the conversion service should be added.
379+
* @return the current builder
380+
* @since 2.1.0
381+
*/
382+
public SpringApplicationBuilder setAddConversionService(
383+
boolean addConversionService) {
384+
this.application.setAddConversionService(addConversionService);
385+
return this;
386+
}
387+
373388
/**
374389
* Default properties for the environment in the form {@code key=value} or
375390
* {@code key:value}.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ApplicationConversionService(StringValueResolver embeddedValueResolver) {
5757
* building it once needed.
5858
* @return the shared {@code ConversionService} instance (never {@code null})
5959
*/
60-
public static ConversionService getSharedInstance() {
60+
public static ApplicationConversionService getSharedInstance() {
6161
ApplicationConversionService sharedInstance = ApplicationConversionService.sharedInstance;
6262
if (sharedInstance == null) {
6363
synchronized (ApplicationConversionService.class) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.boot.context.event.ApplicationStartedEvent;
5757
import org.springframework.boot.context.event.ApplicationStartingEvent;
5858
import org.springframework.boot.context.event.SpringApplicationEvent;
59+
import org.springframework.boot.convert.ApplicationConversionService;
5960
import org.springframework.boot.testsupport.rule.OutputCapture;
6061
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
6162
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
@@ -597,6 +598,28 @@ public void disableCommandLinePropertySource() {
597598
matchingPropertySource(PropertySource.class, "commandLineArgs"));
598599
}
599600

601+
@Test
602+
public void contextUsesApplicationConversionService() {
603+
SpringApplication application = new SpringApplication(ExampleConfig.class);
604+
application.setWebApplicationType(WebApplicationType.NONE);
605+
this.context = application.run();
606+
assertThat(this.context.getBeanFactory().getConversionService())
607+
.isInstanceOf(ApplicationConversionService.class);
608+
assertThat(this.context.getEnvironment().getConversionService())
609+
.isInstanceOf(ApplicationConversionService.class);
610+
}
611+
612+
@Test
613+
public void contextWhenHasAddConversionServiceFalseUsesRegularConversionService() {
614+
SpringApplication application = new SpringApplication(ExampleConfig.class);
615+
application.setWebApplicationType(WebApplicationType.NONE);
616+
application.setAddConversionService(false);
617+
this.context = application.run();
618+
assertThat(this.context.getBeanFactory().getConversionService()).isNull();
619+
assertThat(this.context.getEnvironment().getConversionService())
620+
.isNotInstanceOf(ApplicationConversionService.class);
621+
}
622+
600623
@Test
601624
public void runCommandLineRunnersAndApplicationRunners() {
602625
SpringApplication application = new SpringApplication(CommandLineRunConfig.class);

spring-boot-samples/spring-boot-sample-simple/src/main/java/sample/simple/service/HelloWorldService.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package sample.simple.service;
1818

19+
import java.time.Duration;
20+
1921
import org.springframework.beans.factory.annotation.Value;
2022
import org.springframework.stereotype.Component;
2123

@@ -25,8 +27,15 @@ public class HelloWorldService {
2527
@Value("${name:World}")
2628
private String name;
2729

30+
@Value("${duration:10s}")
31+
private Duration duration;
32+
2833
public String getHelloMessage() {
29-
return "Hello " + this.name;
34+
return "Hello " + this.name + " for " + this.duration.getSeconds() + " seconds";
35+
}
36+
37+
public Duration getDuration() {
38+
return this.duration;
3039
}
3140

3241
}

spring-boot-samples/spring-boot-sample-simple/src/test/java/sample/simple/SampleSimpleApplicationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -62,9 +62,9 @@ public void testDefaultSettings() throws Exception {
6262

6363
@Test
6464
public void testCommandLineOverrides() throws Exception {
65-
SampleSimpleApplication.main(new String[] { "--name=Gordon" });
65+
SampleSimpleApplication.main(new String[] { "--name=Gordon", "--duration=1m" });
6666
String output = this.outputCapture.toString();
67-
assertThat(output).contains("Hello Gordon");
67+
assertThat(output).contains("Hello Gordon for 60 seconds");
6868
}
6969

7070
}

0 commit comments

Comments
 (0)