根据Spring Boot 官方文档的介绍,Spring Boot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。应用开箱即用,只要通过 “just run”(可能是 java -jar 或 tomcat 或 maven插件run 或 shell脚本),就可以启动项目。二者,Spring Boot 只要很少的Spring配置文件。
因为“习惯优先于配置”的原则,使得Spring Boot在快速开发应用和微服务架构实践中得到广泛应用。
在搭建Spring Boot之前需要装好java JDK和maven环境。
获取Maven基础项目
可以手动建立一个Maven的基础项目,也可以直接使用Maven骨架工程生成Maven骨架Web项目,使用man archetype:generate
命令:
mvn archetype:generate -DgroupId=springboot -DartifactId=springboot-helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
修改pom.xml文件
在pom.xml文件中加入Spring Boot需要的相关jar,完整的pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot</groupId>
<artifactId>springboot-helloworld</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>springboot-helloworld</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Controller层代码
package springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2017/9/27.
*/
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello() {
return "Hello,World!";
}
}
@RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。
-
@RestController:提供实现了REST API,可以服务JSON,XML或者其他。这里是以String的形式渲染出结果。
-
@RequestMapping:提供路由信息,”/“路径的HTTP Request都会被映射到sayHello方法进行处理。
启动应用类
package springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
- @SpringBootApplication:Spring Boot 应用的标识
- App很简单,一个main函数作为主入口。SpringApplication引导应用,并将App本身作为参数传递给run方法。具体run方法会启动嵌入式的Tomcat并初始化Spring环境及其各Spring组件。
Controller的测试类
package springboot;
import junit.framework.TestCase;
import org.junit.Test;
import springboot.controller.HelloWorldController;
/**
* Created by Administrator on 2017/9/27.
*/
public class HelloWorldControllerTest extends TestCase {
@Test
public void testSayHello() {
assertEquals("Hello,World!",new HelloWorldController().sayHello());
}
}
启动服务
Just Run的宗旨,运行很简单,直接右键Run运行App类。同样你也可以Debug Run。可以在控制台中看到:
![](https://img.haomeiwen.com/i6430003/b98ac3a08bb20060.png)
表明启动成功,端口是8080.
在浏览器中http://localhost:8080/ 便可以看到结果。
运行测试类
直接右键运行类HelloWorldControllerTest,可以看到测试结果。
全部的代码见github上:https://github.com/liuahang/spring-boot-helloworld
网友评论