diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java b/spring-jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java index 23ad7c54ea..17868d7c4c 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,6 @@ package org.springframework.jdbc.config; -import org.w3c.dom.Element; - import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -27,27 +25,39 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + /** - * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code embedded-database} - * element and creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested - * {@code script} elements and configures a {@link ResourceDatabasePopulator} for them. + * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that + * parses an {@code embedded-database} element and creates a {@link BeanDefinition} + * for an {@link EmbeddedDatabaseFactoryBean}. + * + *
Picks up nested {@code script} elements and configures a
+ * {@link ResourceDatabasePopulator} for each of them.
*
* @author Oliver Gierke
* @author Juergen Hoeller
+ * @author Sam Brannen
* @since 3.0
+ * @see DatabasePopulatorConfigUtils
*/
class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class);
+ useIdAsDatabaseNameIfGiven(element, builder);
setDatabaseType(element, builder);
DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
- useIdAsDatabaseNameIfGiven(element, builder);
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
return builder.getBeanDefinition();
}
+ @Override
+ protected boolean shouldGenerateIdAsFallback() {
+ return true;
+ }
+
private void useIdAsDatabaseNameIfGiven(Element element, BeanDefinitionBuilder builder) {
String id = element.getAttribute(ID_ATTRIBUTE);
if (StringUtils.hasText(id)) {
diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java
index 79b5137ed6..b981da07d3 100644
--- a/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java
+++ b/spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -64,6 +64,11 @@ public class JdbcNamespaceIntegrationTests {
assertCorrectSetup("jdbc-config.xml", "derbyDataSource");
}
+ @Test
+ public void createWithAnonymousDataSource() throws Exception {
+ assertCorrectSetupAndCloseContextForSingleDataSource("jdbc-config-anonymous-datasource.xml", 1);
+ }
+
@Test
public void createWithResourcePattern() throws Exception {
assertCorrectSetup("jdbc-config-pattern.xml", "dataSource");
@@ -169,4 +174,16 @@ public class JdbcNamespaceIntegrationTests {
}
}
+ private void assertCorrectSetupAndCloseContextForSingleDataSource(String file, int count) {
+ ConfigurableApplicationContext context = context(file);
+ try {
+ DataSource dataSource = context.getBean(DataSource.class);
+ JdbcTemplate template = new JdbcTemplate(dataSource);
+ assertNumRowsInTestTable(template, count);
+ }
+ finally {
+ context.close();
+ }
+ }
+
}
diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-anonymous-datasource.xml b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-anonymous-datasource.xml
new file mode 100644
index 0000000000..f27d19a89c
--- /dev/null
+++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-anonymous-datasource.xml
@@ -0,0 +1,12 @@
+
+