25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

jar {<br />

}<br />

baseName = 'ch19-spring-boot'<br />

version = '0.1.0'<br />

repositories {<br />

mavenCentral()<br />

maven {<br />

url "http://repo.spring.io/libs-snapshot"<br />

url 'http://repo.spring.io/milestone'<br />

url 'http://repo.spring.io/libs-release'<br />

}<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-1 shows the build.gradle file. The important part of this file is where you describe the buildscript<br />

because you are going to use a spring-boot plug-in. The spring-boot plug-in has several tasks, one of which is<br />

the bootRun task. Don’t worry too much about these tasks; you are going to use them very soon.<br />

Remember that you still have all the My Documents classes from previous chapters. So let’s reuse them,<br />

including some persistence like the JDBC. And because <strong>Spring</strong> Boot promotes zero configuration files, you are going<br />

to use some Java Config classes, as shown in Listing 19-2.<br />

Listing 19-2. JDBCConfig.java<br />

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

import javax.sql.DataSource;<br />

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

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

import org.springframework.context.annotation.ImportResource;<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 />

@Configuration<br />

@ImportResource("classpath:META-INF/spring/jdbc-context.xml")<br />

public class JDBCConfig {<br />

264

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

Saved successfully!

Ooh no, something went wrong!