From 5c43a5b7dd94656a97783fd478372f582deccd1f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 15 Jun 2016 16:53:59 +0100 Subject: [PATCH] Honour custom bean name generator for non-web applications Closes gh-6160 --- .../springframework/boot/SpringApplication.java | 14 ++++---------- .../boot/SpringApplicationTests.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 73a3f68a9a..1dc3aff452 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -72,7 +72,6 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; -import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.StandardServletEnvironment; @@ -603,15 +602,10 @@ public class SpringApplication { * @param context the application context */ protected void postProcessApplicationContext(ConfigurableApplicationContext context) { - if (this.webEnvironment) { - if (context instanceof ConfigurableWebApplicationContext) { - ConfigurableWebApplicationContext configurableContext = (ConfigurableWebApplicationContext) context; - if (this.beanNameGenerator != null) { - configurableContext.getBeanFactory().registerSingleton( - AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, - this.beanNameGenerator); - } - } + if (this.beanNameGenerator != null) { + context.getBeanFactory().registerSingleton( + AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, + this.beanNameGenerator); } if (this.resourceLoader != null) { if (context instanceof GenericApplicationContext) { diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index dec42064e9..36a64f2769 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -53,6 +53,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.SimpleApplicationEventMulticaster; @@ -403,6 +404,21 @@ public class SpringApplicationTests { sameInstance((Object) beanNameGenerator)); } + @Test + public void customBeanNameGeneratorWithNonWebApplication() throws Exception { + TestSpringApplication application = new TestSpringApplication( + ExampleWebConfig.class); + application.setWebEnvironment(false); + BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); + application.setBeanNameGenerator(beanNameGenerator); + this.context = application.run(); + verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator); + assertThat( + this.context + .getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR), + sameInstance((Object) beanNameGenerator)); + } + @Test public void commandLinePropertySource() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class);