From 4ca26058fadbe93a3766d45f5002b90446ea5648 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 26 Apr 2013 10:39:06 +0100 Subject: [PATCH] [bs-73] Tweak configuration class detection algorithm some more This looks like becoming a recurring theme. Unfortunately it's not very easy to test if a class might eventually be instantiatable by Spring. This change fixes the main bootstrap SpringApplicationTests which were failing because they used non-static inner classes for test configurations. --- .../springframework/bootstrap/BeanDefinitionLoader.java | 7 +++++++ .../springframework/bootstrap/SpringApplicationTests.java | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java index 53f66aa3ec..39f4d43c78 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/BeanDefinitionLoader.java @@ -24,12 +24,14 @@ import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter; import org.springframework.core.type.filter.TypeFilter; +import org.springframework.stereotype.Component; import org.springframework.util.Assert; /** @@ -154,6 +156,11 @@ class BeanDefinitionLoader { } private boolean isComponent(Class type) { + // This has to be a bit of a guess. The only way to be sure that this type is + // eligible is to make a bean definition out of it and try to instantiate it. + if (AnnotationUtils.findAnnotation(type, Component.class) != null) { + return true; + } // Nested anonymous classes are not eligible for registration, nor are groovy // closures if (type.isAnonymousClass() || type.getName().contains("$_closure") diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java index 81fdae2ce0..372ffd1b48 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/SpringApplicationTests.java @@ -24,7 +24,6 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultBeanNameGenerator; -import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration; import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.context.ApplicationContext; @@ -331,7 +330,6 @@ public class SpringApplicationTests { } @Configuration - @EnableAutoConfiguration static class ExampleWebConfig { @Bean