25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 19 ■ <strong>Spring</strong> Boot, Simplifying Everything<br />

configurations {<br />

providedRuntime<br />

}<br />

dependencies {<br />

compile("org.springframework.boot:spring-boot-starter-web") {<br />

exclude module: "spring-boot-starter-tomcat"<br />

}<br />

compile("org.springframework.boot:spring-boot-starter-jetty")<br />

compile("org.springframework.boot:spring-boot-starter-actuator")<br />

compile("org.springframework.boot:spring-boot-starter-jdbc")<br />

}<br />

runtime 'hsqldb:hsqldb:1.8.0.10'<br />

testCompile("junit:junit")<br />

Listing 19-5 shows how to create a WAR file that can run on any application server. As you can see, there are<br />

several sections: apply plugin: 'war', war, and configurations. In the war section, you provided the base name<br />

and the version of the WAR file; in the configurations section, you added the providedRuntime keyword. This<br />

keyword tells the gradle not to package jars that are already part of the application server.<br />

Next, you are going to create a new class that will be the entry point for <strong>Spring</strong> Boot. This call will know how to<br />

add dependencies and how to wire everything up for the WAR application. See Listing 19-6.<br />

Listing 19-6. MyDocumentsWebApp.java<br />

package com.apress.isf.spring.boot;<br />

import javax.sql.DataSource;<br />

import org.springframework.boot.<strong>Spring</strong>Application;<br />

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;<br />

import org.springframework.context.annotation.Bean;<br />

import org.springframework.context.annotation.ComponentScan;<br />

import org.springframework.context.annotation.Configuration;<br />

import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;<br />

import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;<br />

import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;<br />

import org.springframework.boot.builder.<strong>Spring</strong>ApplicationBuilder;<br />

import org.springframework.boot.context.web.<strong>Spring</strong>BootServletInitializer;<br />

@Configuration<br />

@EnableAutoConfiguration<br />

@ComponentScan("com.apress.isf.spring")<br />

public class MyDocumentsWebApp extends <strong>Spring</strong>BootServletInitializer{<br />

@Bean<br />

public DataSource dataSource() {<br />

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();<br />

EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).<br />

addScript("META-INF/data/schema.sql").<br />

addScript("META-INF/data/data.sql").build();<br />

return db;<br />

}<br />

271

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!