Skip to content

Commit 7e2f12c

Browse files
committed
Allow <jdbc:embedded-database> to be declared w/o id
This commit modifies EmbeddedDatabaseBeanDefinitionParser so that the <jdbc:embedded-database> XML namespace element can be declared as an anonymous bean (i.e., without an explicit ID). Issue: SPR-12834
1 parent 85e4b24 commit 7e2f12c

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

Diff for: spring-jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.jdbc.config;
1818

19-
import org.w3c.dom.Element;
20-
2119
import org.springframework.beans.factory.config.BeanDefinition;
2220
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2321
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -27,27 +25,39 @@
2725
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2826
import org.springframework.util.StringUtils;
2927

28+
import org.w3c.dom.Element;
29+
3030
/**
31-
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code embedded-database}
32-
* element and creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested
33-
* {@code script} elements and configures a {@link ResourceDatabasePopulator} for them.
31+
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that
32+
* parses an {@code embedded-database} element and creates a {@link BeanDefinition}
33+
* for an {@link EmbeddedDatabaseFactoryBean}.
34+
*
35+
* <p>Picks up nested {@code script} elements and configures a
36+
* {@link ResourceDatabasePopulator} for each of them.
3437
*
3538
* @author Oliver Gierke
3639
* @author Juergen Hoeller
40+
* @author Sam Brannen
3741
* @since 3.0
42+
* @see DatabasePopulatorConfigUtils
3843
*/
3944
class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
4045

4146
@Override
4247
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
4348
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class);
49+
useIdAsDatabaseNameIfGiven(element, builder);
4450
setDatabaseType(element, builder);
4551
DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
46-
useIdAsDatabaseNameIfGiven(element, builder);
4752
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
4853
return builder.getBeanDefinition();
4954
}
5055

56+
@Override
57+
protected boolean shouldGenerateIdAsFallback() {
58+
return true;
59+
}
60+
5161
private void useIdAsDatabaseNameIfGiven(Element element, BeanDefinitionBuilder builder) {
5262
String id = element.getAttribute(ID_ATTRIBUTE);
5363
if (StringUtils.hasText(id)) {

Diff for: spring-jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,6 +64,11 @@ public void createEmbeddedDatabaseAgain() throws Exception {
6464
assertCorrectSetup("jdbc-config.xml", "derbyDataSource");
6565
}
6666

67+
@Test
68+
public void createWithAnonymousDataSource() throws Exception {
69+
assertCorrectSetupAndCloseContextForSingleDataSource("jdbc-config-anonymous-datasource.xml", 1);
70+
}
71+
6772
@Test
6873
public void createWithResourcePattern() throws Exception {
6974
assertCorrectSetup("jdbc-config-pattern.xml", "dataSource");
@@ -169,4 +174,16 @@ private void assertCorrectSetupAndCloseContext(String file, int count, String...
169174
}
170175
}
171176

177+
private void assertCorrectSetupAndCloseContextForSingleDataSource(String file, int count) {
178+
ConfigurableApplicationContext context = context(file);
179+
try {
180+
DataSource dataSource = context.getBean(DataSource.class);
181+
JdbcTemplate template = new JdbcTemplate(dataSource);
182+
assertNumRowsInTestTable(template, count);
183+
}
184+
finally {
185+
context.close();
186+
}
187+
}
188+
172189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd">
6+
7+
<jdbc:embedded-database>
8+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" />
9+
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql" />
10+
</jdbc:embedded-database>
11+
12+
</beans>

0 commit comments

Comments
 (0)