Implementing the Builder pattern to create an embedded data source
In application development, the embedded database is very useful, because it doesn't require a separate database server that your application connects. Spring provides one more data source for embedded databases. It is not powerful enough for the production environment. We can use the embedded data source for the development and testing environment. In Spring, the jdbc
namespace configures an embedded database, H2
, as follows:
In XML configuration, H2
is configured as follows:
<jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="schema.sql"/> <jdbc:script location="data.sql"/> </jdbc:embedded-database>
In Java configuration, H2
is configured as follows:
@Bean public DataSource dataSource(){ EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2); builder.addScript("schema.sql"); builder...