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 />

}<br />

@Override<br />

protected <strong>Spring</strong>ApplicationBuilder configure(<strong>Spring</strong>ApplicationBuilder application) {<br />

return application.sources(MyDocumentsWebApp.class);<br />

}<br />

Listing 19-6 shows the new class. You are now extending from the <strong>Spring</strong>BootServletInitializer class and you<br />

are overriding a configure method. In this method, you are returning an application that will tell <strong>Spring</strong> Boot how<br />

to initialize all web-related files. Also, note that you are using the @Configuration, @EnableAutoConfiguration, and<br />

@ComponentScan annotations. These annotations are essential for <strong>Spring</strong> Boot; they’re the way it looks for all of the<br />

necessary components for the web application.<br />

Before in the JAR application (Listing 19-4), <strong>Spring</strong> Boot recognized that it would be a JAR application because it<br />

had a main method and it was executing the <strong>Spring</strong>Application.run method. This time, <strong>Spring</strong> Boot will identify this<br />

application as a web app because you are extending from the <strong>Spring</strong>BootServletInitializer class and overriding<br />

the configure method, and it will wire everything up so it can run on any Java web container.<br />

The following command will create the WAR file in the build/libs folder:<br />

gradle clean :ch19:build<br />

Now you can deploy the WAR file on any application server or Java web container. If you are deploying in a<br />

Tomcat-like server, you need to remember that the context by default is the name of the WAR, so the URL will be<br />

http://localhost:8080/ch19-spring-boot-0.1.0/documents.<br />

<strong>Spring</strong> Boot À La Groovy<br />

<strong>Spring</strong> Boot also plays well with Groovy, so you are going to create just a subset of the My Documents <strong>Spring</strong> application,<br />

based on Groovy scripts—only the access to the in-memory database. See Listing 19-7 for the model.groovy file.<br />

Listing 19-7. model.groovy<br />

class Document {<br />

String documentId<br />

String name<br />

Type type<br />

String location<br />

String description<br />

Date created<br />

Date modified<br />

}<br />

class Type {<br />

String typeId<br />

String name<br />

String desc<br />

String extension<br />

}<br />

272

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

Saved successfully!

Ooh no, something went wrong!