Spring Boot 入门篇

    xiaoxiao2026-05-24  12

    Spring Boot 相比Spring Framework框架的繁杂配置(各种XML配置与代码CP)更轻量化,内嵌Web 容器(默认Tomcat)启动应用只需一个类即可;使Spring开发者能更快的入门,大大的提高了开发效率, 下文开始演示Spring Boot的入门(英文与技术基础好的同学,可以直进入官网看demo学习)。

    开发环境说明:

    java version "1.8.0_91"Spring Boot 1.5.2.RELEASE用例亲自调试通过,放心入坑。 Maven构建POM

    代码引用

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!-- 引入Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> Gradle构建POM

    代码引用

    dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE") }

    项目目录结构

    新建类

    hello/SampleController.java

    代码引用

    package hello; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }

    启动应用主程序SampleController

    启动日志

    启动默认用了Tomcat 端口8080Spring Boot 版本号1.5.2无需任何配置文件申明

    下面开始使用Postman来请求应用,结果如图也可以直接在浏览器上面输入:http://localhost:8080/

    'Hello World!'

    本项目演示了,通过Maven 构建项目,引用Spring Boot中

    spring-boot-starter-parent spring-boot-starter-web

    实现了基础的Web应用,处理简单的请求,下篇开始引用application.properties 或 application.yml 实现修改应用默认端口与数据库访问。

    参考资料:

    http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/

    相关资源:spring boot入门篇demo ppt
    最新回复(0)