Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 1 and 2: THE EXPERT’S VOICE ® IN SPRING
- Page 3 and 4: Contents at a Glance About the Auth
- Page 5 and 6: Introduction This book is an introd
- Page 7 and 8: Part I Spring Framework Basics The
- Page 9: Chapter 1 ■ Your First Spring App
- Page 13 and 14: Chapter 1 ■ Your First Spring App
- Page 15 and 16: Chapter 1 ■ Your First Spring App
- Page 17 and 18: Chapter 1 ■ Your First Spring App
- Page 19 and 20: Chapter 2 ■ Working with Classes
- Page 21 and 22: Chapter 2 ■ Working with Classes
- Page 23 and 24: Chapter 2 ■ Working with Classes
- Page 25 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
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