|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.lang.reflect.Constructor; |
20 | 21 | import java.util.HashSet;
|
21 | 22 | import java.util.Set;
|
22 | 23 |
|
|
31 | 32 | import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
32 | 33 | import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
33 | 34 | import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
34 |
| -import org.springframework.core.annotation.MergedAnnotations; |
35 |
| -import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; |
36 | 35 | import org.springframework.core.env.ConfigurableEnvironment;
|
37 | 36 | import org.springframework.core.io.ClassPathResource;
|
38 | 37 | import org.springframework.core.io.Resource;
|
|
41 | 40 | import org.springframework.core.io.support.ResourcePatternResolver;
|
42 | 41 | import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
|
43 | 42 | import org.springframework.core.type.filter.TypeFilter;
|
44 |
| -import org.springframework.stereotype.Component; |
45 | 43 | import org.springframework.util.Assert;
|
46 | 44 | import org.springframework.util.ClassUtils;
|
| 45 | +import org.springframework.util.ObjectUtils; |
47 | 46 | import org.springframework.util.StringUtils;
|
48 | 47 |
|
49 | 48 | /**
|
|
53 | 52 | * {@link SpringApplication} for the types of sources that are supported.
|
54 | 53 | *
|
55 | 54 | * @author Phillip Webb
|
| 55 | + * @author Vladislav Kisel |
56 | 56 | * @see #setBeanNameGenerator(BeanNameGenerator)
|
57 | 57 | */
|
58 | 58 | class BeanDefinitionLoader {
|
@@ -153,7 +153,7 @@ private int load(Class<?> source) {
|
153 | 153 | GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
|
154 | 154 | load(loader);
|
155 | 155 | }
|
156 |
| - if (isComponent(source)) { |
| 156 | + if (isEligible(source)) { |
157 | 157 | this.annotatedReader.register(source);
|
158 | 158 | return 1;
|
159 | 159 | }
|
@@ -273,16 +273,23 @@ private Package findPackage(CharSequence source) {
|
273 | 273 | return Package.getPackage(source.toString());
|
274 | 274 | }
|
275 | 275 |
|
276 |
| - private boolean isComponent(Class<?> type) { |
277 |
| - // This has to be a bit of a guess. The only way to be sure that this type is |
278 |
| - // eligible is to make a bean definition out of it and try to instantiate it. |
279 |
| - if (MergedAnnotations.from(type, SearchStrategy.TYPE_HIERARCHY).isPresent(Component.class)) { |
280 |
| - return true; |
281 |
| - } |
282 |
| - // Nested anonymous classes are not eligible for registration, nor are groovy |
283 |
| - // closures |
284 |
| - return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass() |
285 |
| - && type.getConstructors() != null && type.getConstructors().length != 0; |
| 276 | + /** |
| 277 | + * Check whether the bean is eligible for registration. |
| 278 | + * @param type candidate bean type |
| 279 | + * @return true if the given bean type is eligible for registration, i.e. not a groovy |
| 280 | + * closure nor an anonymous class |
| 281 | + */ |
| 282 | + private boolean isEligible(Class<?> type) { |
| 283 | + return !(type.isAnonymousClass() || isGroovyClosure(type) || hasNoConstructors(type)); |
| 284 | + } |
| 285 | + |
| 286 | + private boolean isGroovyClosure(Class<?> type) { |
| 287 | + return type.getName().matches(".*\\$_.*closure.*"); |
| 288 | + } |
| 289 | + |
| 290 | + private boolean hasNoConstructors(Class<?> type) { |
| 291 | + Constructor<?>[] constructors = type.getDeclaredConstructors(); |
| 292 | + return ObjectUtils.isEmpty(constructors); |
286 | 293 | }
|
287 | 294 |
|
288 | 295 | /**
|
|
0 commit comments