Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 1 ■ Your First Spring Application } url "http://repo.spring.io/libs-snapshot" url 'http://repo.spring.io/milestone' url 'http://repo.spring.io/libs-release' } Listing 1-5 shows that instead of adding the Spring Framework dependency, now you are using a spring-boot-starter that will actually help to wire up everything to run this application as a web application. Listing 1-6 shows your main class, the HelloWorldController; this class introduces new annotations that will help the Spring container know what to do and create the necessary collaboration classes and run it as a web application. Listing 1-6. HelloWorldController.java package com.apress.isf.spring; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class HelloWorldController { @RequestMapping("/") @ResponseBody String getMessage() { return "Hello World!"; } } public static void main(String[] args) throws Exception { SpringApplication.run(HelloWorldController.class, args); } The @Controller annotation will mark your class as a web controller that has a @RequestMapping and a @ResponseBody. All this means is that when your web application is running, it will accept requests from the http://localhost:8080/ URL and you should get some response back, such as the famous “Hello World” message. Running the Spring Boot Application Run the HelloWorldClass with the following command: gradle - run -DmainClass=com.apress.isf.spring.HelloWorldController 9
Chapter 1 ■ Your First Spring Application You should have the following output after executing the gradle command: isf-book$ gradle run -DmainClass=com.apress.isf.spring.HelloWorldController :ch01:compileJava UP-TO-DATE :ch01:compileGroovy UP-TO-DATE :ch01:processResources UP-TO-DATE :ch01:classes UP-TO-DATE :ch01:run :: Spring Boot :: (v1.0.2.RELEASE) INFO 84872 --- [main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 INFO 84872 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat INFO 84872 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52 INFO 84872 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext INFO 84872 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2030 ms INFO 84872 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http INFO 84872 --- [main] c.a.isf.spring.HelloWorldController : Started HelloWorldController in 7.086 seconds (JVM running for 7.599) > Building 80% > :ch01:run Now you can go to any browser and type in the URL http://localhost:8080 (see Figure 1-3). Figure 1-3. Spring Boot Web Page 10
- 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 and 10: Chapter 1 ■ Your First Spring App
- Page 11 and 12: Chapter 1 ■ Your First Spring App
- Page 13: 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
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
Chapter 1 ■ Your First <strong>Spring</strong> Application<br />
}<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 />
Listing 1-5 shows that instead of adding the <strong>Spring</strong> <strong>Framework</strong> dependency, now you are using a<br />
spring-boot-starter that will actually help to wire up everything to run this application as a web application.<br />
Listing 1-6 shows your main class, the HelloWorldController; this class introduces new annotations that<br />
will help the <strong>Spring</strong> container know what to do and create the necessary collaboration classes and run it as a web<br />
application.<br />
Listing 1-6. HelloWorldController.java<br />
package com.apress.isf.spring;<br />
import org.springframework.boot.<strong>Spring</strong>Application;<br />
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;<br />
import org.springframework.stereotype.Controller;<br />
import org.springframework.web.bind.annotation.RequestMapping;<br />
import org.springframework.web.bind.annotation.ResponseBody;<br />
@Controller<br />
@EnableAutoConfiguration<br />
public class HelloWorldController {<br />
@RequestMapping("/")<br />
@ResponseBody<br />
String getMessage() {<br />
return "Hello World!";<br />
}<br />
}<br />
public static void main(String[] args) throws Exception {<br />
<strong>Spring</strong>Application.run(HelloWorldController.class, args);<br />
}<br />
The @Controller annotation will mark your class as a web controller that has a @RequestMapping and a<br />
@ResponseBody. All this means is that when your web application is running, it will accept requests from the<br />
http://localhost:8080/ URL and you should get some response back, such as the famous “Hello World” message.<br />
Running the <strong>Spring</strong> Boot Application<br />
Run the HelloWorldClass with the following command:<br />
gradle - run -DmainClass=com.apress.isf.spring.HelloWorldController<br />
9