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 1 ■ Your First <strong>Spring</strong> Application<br />

Figure 1-3 shows the result of running the HelloWorldController.java file. <strong>Spring</strong> Boot will know what to do to<br />

create a web context and response to any request based on the annotation provided. But why am I showing this? Well<br />

if you know how to create a Java web application, you can see that <strong>Spring</strong> Boot simplifies everything because you’re<br />

not writing any configuration files for a Java web application; it’s configuration free!<br />

<strong>Spring</strong> Boot, à la Groovy<br />

<strong>Spring</strong> Boot also provides a powerful interaction with the Groovy programming language, making it even easier to<br />

create applications. But wait! Groovy? What is Groovy? The Groovy programming language is based on Java and of<br />

course is run on the JVM. If you know Java, you already know Groovy. Groovy is a dynamic programming language<br />

that gets rid of all of the boilerplate of any Java class and adds extensible methods to existing Java classes, making it a<br />

powerful language.<br />

I am not going into the details of the Groovy language here, but I will say that it is one of the languages in the JVM<br />

community that has gathered many followers and developers. So the <strong>Spring</strong> Team has made the interaction between<br />

Groovy and the <strong>Spring</strong> <strong>Framework</strong> possible.<br />

Let’s continue with the next example. You are going to create only one file; there is no need to create any structure<br />

as before. Only one file:<br />

• app.groovy<br />

Listing 1-7 shows the app.groovy file.<br />

Listing 1-7. app.groovy<br />

@Controller<br />

class MyApp {<br />

}<br />

@RequestMapping("/")<br />

@ResponseBody<br />

String message() {<br />

return "Hello World!"<br />

}<br />

But why is Listing 1-7 different from Java? I just said that “if you know Java, you know Groovy.” Well, if you add the<br />

public keywords to the class and the method, and you put a semicolon after every statement, you have a Java class.<br />

Note that you are still using some annotations like @controller, @RequestMapping, and @responseBody, similar to<br />

Listing 1-6. But now you are not using any imports! That’s right! There is no package and there are no imports. <strong>Spring</strong><br />

Boot will recognize all the annotations used and it will integrate all the libraries in this simple application.<br />

Let’s run the code in Listing 1-7, but before you do so, you need to install <strong>Spring</strong> Boot (see Appendix A for details<br />

on how to install <strong>Spring</strong> Boot on your system). Another difference from the previous example (Listing 1-6) is that you<br />

are not using Gradle this time. You are going to use the <strong>Spring</strong> Boot runtime environment, a command-line tool that<br />

will help you to build and run Groovy examples.<br />

Once you have installed <strong>Spring</strong> Boot, you can execute the following command:<br />

spring run app.groovy<br />

11

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

Saved successfully!

Ooh no, something went wrong!