From c36c6cbfaafc5efdcc707d86226acf2258a1bba1 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 20 Mar 2015 16:42:04 +0100 Subject: [PATCH] Introduce database-name in Prior to this commit, the EmbeddedDatabaseBeanDefinitionParser set the name of the embedded database that it configured to the value of its 'id'. This made it impossible to assign unique names to embedded databases if the same bean 'id' (e.g, 'dataSource') was used across multiple application contexts loaded within the same JVM, which is often the case within an integration test suite. In contrast, the EmbeddedDatabaseBuilder already provides support for setting the name in Java Config. Thus there is a disconnect between XML and Java configuration. This commit addresses this issue by introducing a 'database-name' attribute in . This allows developers to set unique names for embedded databases -- for example, via a SpEL expression or a property placeholder that is influenced by the current active bean definition profiles. Issue: SPR-12835 --- .../EmbeddedDatabaseBeanDefinitionParser.java | 25 ++++++++++--- .../jdbc/config/spring-jdbc-4.2.xsd | 10 ++++++ .../config/JdbcNamespaceIntegrationTests.java | 35 ++++++++++++++----- ...name-default-and-anonymous-datasource.xml} | 0 .../config/jdbc-config-db-name-explicit.xml | 12 +++++++ .../config/jdbc-config-db-name-implicit.xml | 12 +++++++ 6 files changed, 80 insertions(+), 14 deletions(-) rename spring-jdbc/src/test/resources/org/springframework/jdbc/config/{jdbc-config-anonymous-datasource.xml => jdbc-config-db-name-default-and-anonymous-datasource.xml} (100%) create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-db-name-explicit.xml create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-config-db-name-implicit.xml 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 17868d7c4c..5093a52732 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 @@ -43,10 +43,16 @@ import org.w3c.dom.Element; */ class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser { + /** + * Constant for the "database-name" attribute. + */ + static final String DB_NAME_ATTRIBUTE = "database-name"; + + @Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class); - useIdAsDatabaseNameIfGiven(element, builder); + setDatabaseName(element, builder); setDatabaseType(element, builder); DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder); builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); @@ -58,11 +64,20 @@ class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser return true; } - private void useIdAsDatabaseNameIfGiven(Element element, BeanDefinitionBuilder builder) { - String id = element.getAttribute(ID_ATTRIBUTE); - if (StringUtils.hasText(id)) { - builder.addPropertyValue("databaseName", id); + private void setDatabaseName(Element element, BeanDefinitionBuilder builder) { + // 1) Check for an explicit database name + String name = element.getAttribute(DB_NAME_ATTRIBUTE); + + // 2) Fall back to an implicit database name based on the ID + if (!StringUtils.hasText(name)) { + name = element.getAttribute(ID_ATTRIBUTE); } + + if (StringUtils.hasText(name)) { + builder.addPropertyValue("databaseName", name); + } + + // 3) Let EmbeddedDatabaseFactory set the default "testdb" name } private void setDatabaseType(Element element, BeanDefinitionBuilder builder) { diff --git a/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.2.xsd b/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.2.xsd index 0808d3d569..c307361c68 100644 --- a/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.2.xsd +++ b/spring-jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-4.2.xsd @@ -34,6 +34,16 @@ + + + + + + + + +