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.addScript("data.sql"); return builder.build(); }

As you can see in the preceding code, Spring provides the EmbeddedDatabaseBuilder class. It actually implements the Builder design pattern to create the object of the EmbeddedDatabaseBuilder class.

Let's see one more design pattern in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset