spring-jdbc xsd initial commit; includes embedded-database tag

This commit is contained in:
Keith Donald 2009-05-08 19:06:58 +00:00
parent 3e9b9a8a2a
commit 6cec37ac7f
7 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,65 @@
package org.springframework.jdbc.config;
import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.embedded.DatabasePopulator;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean;
import org.springframework.jdbc.datasource.embedded.ResourceDatabasePopulator;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@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.
*
* @author Oliver Gierke
*/
public class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class);
setDatabaseType(element, builder);
setDatabasePopulator(element, context, builder);
return getSourcedBeanDefinition(builder, element, context);
}
private void setDatabaseType(Element element, BeanDefinitionBuilder builder) {
String type = element.getAttribute("type");
if (StringUtils.hasText(type)) {
builder.addPropertyValue("databaseType", type);
}
}
private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) {
List<Element> scripts = DomUtils.getChildElementsByTagName(element, "script");
if (scripts.size() > 0) {
builder.addPropertyValue("databasePopulator", createDatabasePopulator(scripts, context));
}
}
private DatabasePopulator createDatabasePopulator(List<Element> scripts, ParserContext context) {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
for (Element scriptElement : scripts) {
Resource script = context.getReaderContext().getResourceLoader().getResource(scriptElement.getAttribute("location"));
populator.addScript(script);
}
return populator;
}
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
ParserContext context) {
AbstractBeanDefinition definition = builder.getBeanDefinition();
definition.setSource(context.extractSource(source));
return definition;
}
}

View File

@ -0,0 +1,15 @@
package org.springframework.jdbc.config;
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* {@link NamespaceHandler} for JDBC configuration namespace.
* @author Oliver Gierke
*/
public class JdbcNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("embedded-database", new EmbeddedDatabaseBeanDefinitionParser());
}
}

View File

@ -0,0 +1 @@
http\://www.springframework.org/schema/jdbc=org.springframework.jdbc.config.JdbcNamespaceHandler

View File

@ -0,0 +1 @@
http\://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.springframework.org/schema/jdbc"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/jdbc"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:element name="embedded-database">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.jdbc.embedded.EmbeddedDataSourceFactoryBean"><![CDATA[
Creates an embedded database instance and makes it available to other beans as a javax.sql.DataSource.
]]></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:exports type="javax.sql.DataSource" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:sequence>
<xsd:element name="script" type="scriptType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation><![CDATA[
A SQL script to execute during embedded database initialization.
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="type" type="databaseType">
<xsd:annotation>
<xsd:documentation><![CDATA[
The type of embedded database to create, such as HSQL or H2.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="scriptType">
<xsd:attribute name="location" type="xsd:string">
<xsd:annotation>
<xsd:documentation source="java:org.springframework.core.io.Resource"><![CDATA[
The resource location of the SQL script to execute.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="databaseType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="HSQL" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

View File

@ -0,0 +1,20 @@
package org.springframework.jdbc.config;
import static org.junit.Assert.assertEquals;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcNamespaceIntegrationTest {
@Test
public void testCreateEmbeddedDatabase() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/jdbc/config/jdbc-config.xml");
DataSource ds = context.getBean("dataSource", DataSource.class);
JdbcTemplate t = new JdbcTemplate(ds);
assertEquals(1, t.queryForInt("select count(*) from T_TEST"));
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:org/springframework/jdbc/datasource/embedded/db-schema.sql"/>
<jdbc:script location="classpath:org/springframework/jdbc/datasource/embedded/db-test-data.sql"/>
</jdbc:embedded-database>
</beans>