Add support for Groovy bean builder sources
This commit is contained in:
parent
d635d8af72
commit
0d0de05ae6
|
@ -49,6 +49,11 @@
|
||||||
<artifactId>tomcat-embed-core</artifactId>
|
<artifactId>tomcat-embed-core</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.codehaus.groovy</groupId>
|
||||||
|
<artifactId>groovy</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
<artifactId>jetty-webapp</artifactId>
|
<artifactId>jetty-webapp</artifactId>
|
||||||
|
|
|
@ -20,6 +20,8 @@ import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||||
|
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
|
@ -57,6 +59,8 @@ class BeanDefinitionLoader {
|
||||||
|
|
||||||
private XmlBeanDefinitionReader xmlReader;
|
private XmlBeanDefinitionReader xmlReader;
|
||||||
|
|
||||||
|
private GroovyBeanDefinitionReader groovyReader;
|
||||||
|
|
||||||
private ClassPathBeanDefinitionScanner scanner;
|
private ClassPathBeanDefinitionScanner scanner;
|
||||||
|
|
||||||
private ResourceLoader resourceLoader;
|
private ResourceLoader resourceLoader;
|
||||||
|
@ -73,6 +77,9 @@ class BeanDefinitionLoader {
|
||||||
this.sources = sources;
|
this.sources = sources;
|
||||||
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
|
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
|
||||||
this.xmlReader = new XmlBeanDefinitionReader(registry);
|
this.xmlReader = new XmlBeanDefinitionReader(registry);
|
||||||
|
if (ClassUtils.isPresent("groovy.lang.MetaClass", null)) {
|
||||||
|
this.groovyReader = new GroovyBeanDefinitionReader(this.xmlReader);
|
||||||
|
}
|
||||||
this.scanner = new ClassPathBeanDefinitionScanner(registry);
|
this.scanner = new ClassPathBeanDefinitionScanner(registry);
|
||||||
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
|
this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
|
||||||
}
|
}
|
||||||
|
@ -145,6 +152,13 @@ class BeanDefinitionLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int load(Resource source) {
|
private int load(Resource source) {
|
||||||
|
if (source.getFilename().endsWith(".groovy")) {
|
||||||
|
if (this.groovyReader == null) {
|
||||||
|
throw new BeanDefinitionStoreException(
|
||||||
|
"Cannot load Groovy beans without Groovy on classpath");
|
||||||
|
}
|
||||||
|
return this.groovyReader.loadBeanDefinitions(source);
|
||||||
|
}
|
||||||
return this.xmlReader.loadBeanDefinitions(source);
|
return this.xmlReader.loadBeanDefinitions(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -169,7 +183,7 @@ class BeanDefinitionLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt as resources
|
// Attempt as resources
|
||||||
Resource[] resources = loadResources(resolvedSource);
|
Resource[] resources = findResources(resolvedSource);
|
||||||
int loadCount = 0;
|
int loadCount = 0;
|
||||||
boolean atLeastOneResourceExists = false;
|
boolean atLeastOneResourceExists = false;
|
||||||
for (Resource resource : resources) {
|
for (Resource resource : resources) {
|
||||||
|
@ -191,7 +205,7 @@ class BeanDefinitionLoader {
|
||||||
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
|
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource[] loadResources(String source) {
|
private Resource[] findResources(String source) {
|
||||||
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
|
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
|
||||||
: DEFAULT_RESOURCE_LOADER;
|
: DEFAULT_RESOURCE_LOADER;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -61,6 +61,17 @@ public class BeanDefinitionLoaderTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadGroovyResource() throws Exception {
|
||||||
|
ClassPathResource resource = new ClassPathResource("sample-beans.groovy",
|
||||||
|
getClass());
|
||||||
|
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
|
||||||
|
int loaded = loader.load();
|
||||||
|
assertThat(loaded, equalTo(1));
|
||||||
|
assertTrue(this.registry.containsBean("myGroovyComponent"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadPackage() throws Exception {
|
public void loadPackage() throws Exception {
|
||||||
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
||||||
|
@ -88,6 +99,15 @@ public class BeanDefinitionLoaderTests {
|
||||||
assertTrue(this.registry.containsBean("myXmlComponent"));
|
assertTrue(this.registry.containsBean("myXmlComponent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadGroovyName() throws Exception {
|
||||||
|
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
||||||
|
"classpath:org/springframework/boot/sample-beans.groovy");
|
||||||
|
int loaded = loader.load();
|
||||||
|
assertThat(loaded, equalTo(1));
|
||||||
|
assertTrue(this.registry.containsBean("myGroovyComponent"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void loadPackageName() throws Exception {
|
public void loadPackageName() throws Exception {
|
||||||
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
import org.springframework.boot.sampleconfig.MyComponent;
|
||||||
|
|
||||||
|
beans {
|
||||||
|
myGroovyComponent(MyComponent) {}
|
||||||
|
}
|
Loading…
Reference in New Issue