Skip to content

Commit 5c43a5b

Browse files
committed
Honour custom bean name generator for non-web applications
Closes gh-6160
1 parent ed2586d commit 5c43a5b

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.springframework.util.ReflectionUtils;
7373
import org.springframework.util.StopWatch;
7474
import org.springframework.util.StringUtils;
75-
import org.springframework.web.context.ConfigurableWebApplicationContext;
7675
import org.springframework.web.context.WebApplicationContext;
7776
import org.springframework.web.context.support.StandardServletEnvironment;
7877

@@ -603,15 +602,10 @@ protected ConfigurableApplicationContext createApplicationContext() {
603602
* @param context the application context
604603
*/
605604
protected void postProcessApplicationContext(ConfigurableApplicationContext context) {
606-
if (this.webEnvironment) {
607-
if (context instanceof ConfigurableWebApplicationContext) {
608-
ConfigurableWebApplicationContext configurableContext = (ConfigurableWebApplicationContext) context;
609-
if (this.beanNameGenerator != null) {
610-
configurableContext.getBeanFactory().registerSingleton(
611-
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
612-
this.beanNameGenerator);
613-
}
614-
}
605+
if (this.beanNameGenerator != null) {
606+
context.getBeanFactory().registerSingleton(
607+
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR,
608+
this.beanNameGenerator);
615609
}
616610
if (this.resourceLoader != null) {
617611
if (context instanceof GenericApplicationContext) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5454
import org.springframework.context.annotation.AnnotationConfigUtils;
5555
import org.springframework.context.annotation.Bean;
56+
import org.springframework.context.annotation.ComponentScan;
5657
import org.springframework.context.annotation.Configuration;
5758
import org.springframework.context.event.ContextRefreshedEvent;
5859
import org.springframework.context.event.SimpleApplicationEventMulticaster;
@@ -403,6 +404,21 @@ public void customBeanNameGenerator() throws Exception {
403404
sameInstance((Object) beanNameGenerator));
404405
}
405406

407+
@Test
408+
public void customBeanNameGeneratorWithNonWebApplication() throws Exception {
409+
TestSpringApplication application = new TestSpringApplication(
410+
ExampleWebConfig.class);
411+
application.setWebEnvironment(false);
412+
BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
413+
application.setBeanNameGenerator(beanNameGenerator);
414+
this.context = application.run();
415+
verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator);
416+
assertThat(
417+
this.context
418+
.getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR),
419+
sameInstance((Object) beanNameGenerator));
420+
}
421+
406422
@Test
407423
public void commandLinePropertySource() throws Exception {
408424
SpringApplication application = new SpringApplication(ExampleConfig.class);

0 commit comments

Comments
 (0)