Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 1 ■ Your First Spring Application ■ ■Note Appendix A shows you how to install Gradle. Hello World Example Let’s start with the famous and well known “Hello World” example for your first Spring Application. You need to create the following folder structure (either in Windows or Unix). • build.gradle ••• src ••• main ••• java ••• com ••• apress ••• isf ••• spring • Application.java • HelloWorldMessage.java • MessageService.java Why do you need this folder structure? Gradle follows a community standard for creating Java applications, and primarily is based on Maven (an XML build tool that still is widely used) and a convention. So everything that belongs to the src/main/java folder will be compiled, and the result will be output to a build folder. Listing 1-1 shows your build.gradle file. This file is the key for Gradle to run. In this file, you specify what plug-ins you are going to use. Every plug-in has its own tasks that you can run, such as compile, build, test, jar, etc. Also, you can specify what repositories to use in order to look for the dependencies you specified. In this example, you are going to use the spring-context module version 4.0.5.RELEASE that Gradle will download with all its dependencies. Furthermore, you are telling it that you are going to pass the name of the mainClass in order to run it. Listing 1-1. build.gradle apply plugin: 'java' apply plugin: 'application' mainClassName = System.getProperty("mainClass") repositories { mavenCentral() } dependencies { compile 'org.springframework:spring-context:4.0.5.RELEASE' } 5

Chapter 1 ■ Your First Spring Application Listing 1-2 shows a simple Interface with only one method. Listing 1-2. MessageService.java package com.apress.isf.spring; public interface MessageService { public String getMessage(); } Next, let’s create the HelloWorldMessage that will return just the simple “Hello World” (see Listing 1-3). Listing 1-3. HelloWorldMessage.java package com.apress.isf.spring; public class HelloWorldMessage implements MessageService { } public String getMessage(){ return "Hello World"; } Listing 1-3 shows the implementation of the interface in Listing 1-2. You can make any implementation you want if you keep following the contract that your interface provides. For example, right now you just return a string, but you can actually call a service or go into a database and pick a random message. ■ ■Note All of these examples can be edited using any text editor or any favorite IDE (integrated development environment). Now you are ready to test your implementation, but you need to add a starting point. Running the Hello World Application Listing 1-4 shows the main class where you are going to test your MessageService class implementation (Listing 1-3, the HelloWorldMessage class). Now you need to take a look at Listing 1-4, because I am introducing some annotations (annotations were introduced as a new feature in Java 5). These annotations are markers for the Spring Framework to help understand your classes and they collaborate together. But wait! Spring Framework? Right now you are going to run this example as it is; later in this and the following chapter, you will learn what the Spring Framework is and how it can help you to deliver enterprise-ready applications. Listing 1-4. Application.java package com.apress.isf.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation. AnnotationConfigApplicationContext; 6

Chapter 1 ■ Your First <strong>Spring</strong> Application<br />

■ ■Note Appendix A shows you how to install Gradle.<br />

Hello World Example<br />

Let’s start with the famous and well known “Hello World” example for your first <strong>Spring</strong> Application. You need to create<br />

the following folder structure (either in Windows or Unix).<br />

• build.gradle<br />

••• src<br />

••• main<br />

••• java<br />

••• com<br />

••• apress<br />

••• isf<br />

••• spring<br />

• Application.java<br />

• HelloWorldMessage.java<br />

• MessageService.java<br />

Why do you need this folder structure? Gradle follows a community standard for creating Java applications, and<br />

primarily is based on Maven (an XML build tool that still is widely used) and a convention. So everything that belongs<br />

to the src/main/java folder will be compiled, and the result will be output to a build folder.<br />

Listing 1-1 shows your build.gradle file. This file is the key for Gradle to run. In this file, you specify what plug-ins<br />

you are going to use. Every plug-in has its own tasks that you can run, such as compile, build, test, jar, etc. Also, you<br />

can specify what repositories to use in order to look for the dependencies you specified. In this example, you are<br />

going to use the spring-context module version 4.0.5.RELEASE that Gradle will download with all its dependencies.<br />

Furthermore, you are telling it that you are going to pass the name of the mainClass in order to run it.<br />

Listing 1-1. build.gradle<br />

apply plugin: 'java'<br />

apply plugin: 'application'<br />

mainClassName = System.getProperty("mainClass")<br />

repositories {<br />

mavenCentral()<br />

}<br />

dependencies {<br />

compile 'org.springframework:spring-context:4.0.5.RELEASE'<br />

}<br />

5

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

Saved successfully!

Ooh no, something went wrong!