一 优点
1.创建独立的Spring应用程序
2.嵌入式的Tomcat,不需要部署war包
3.简化Maven配置
4.自动配置Spring
5.提供生产就绪型功能,如指标,健康检查,和外部配置
6.开箱即用,没有代码生成,也无需XML配置
二 代码实现
1.环境要求
JDK1.7及以上,Spring framework 4.1.5及以上
2.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>lf</groupId>
<artifactId>Springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 继承 spring-boot-starter-paren -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入freeMarker的依赖包. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
3.Controller
package com.lf.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @EnableAutoConfiguration//此注释自动载入应用程序所需的所有Bean @RestController//@RestController = @Controller + 每个方法都加上@RequestBody注解 public class HelleController { @RequestMapping("hello") public String hello(){ return "success"; } public static void main(String[] args) { SpringApplication.run(HelleController.class, args); } }
4.访问效果