[bs-73] Anonymous classes cannot be used in @Bean definitions in .groovy scripts
* Added a test for each of the classes loaded by the SpringApplication * If it's an anonymous class or looks like a Groovy closure we ignore it * The CLI sample job.groovy also modified to take advantage [Fixes #48718891]
This commit is contained in:
@@ -116,8 +116,12 @@ class BeanDefinitionLoader {
|
||||
private int load(Object source) {
|
||||
Assert.notNull(source, "Source must not be null");
|
||||
if (source instanceof Class<?>) {
|
||||
this.annotatedReader.register((Class<?>) source);
|
||||
return 1;
|
||||
Class<?> type = (Class<?>) source;
|
||||
if (isComponent(type)) {
|
||||
this.annotatedReader.register(type);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (source instanceof Resource) {
|
||||
@@ -149,6 +153,15 @@ class BeanDefinitionLoader {
|
||||
throw new IllegalArgumentException("Invalid source '" + source + "'");
|
||||
}
|
||||
|
||||
private boolean isComponent(Class<?> type) {
|
||||
// Nested anonymous classes are not eligible for registration, nor are groovy
|
||||
// closures
|
||||
if (type.isAnonymousClass() || type.getName().contains("$_closure")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple {@link TypeFilter} used to ensure that specified {@link Class} sources are
|
||||
* not accidentally re-added during scanning.
|
||||
|
||||
@@ -297,11 +297,11 @@ public class SpringApplication {
|
||||
}
|
||||
}
|
||||
|
||||
if (context instanceof AbstractApplicationContext) {
|
||||
if (context instanceof AbstractApplicationContext && this.environment != null) {
|
||||
((AbstractApplicationContext) context).setEnvironment(this.environment);
|
||||
}
|
||||
|
||||
if (context instanceof GenericApplicationContext) {
|
||||
if (context instanceof GenericApplicationContext && this.resourceLoader != null) {
|
||||
((GenericApplicationContext) context).setResourceLoader(this.resourceLoader);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user