为了快速学习 SpringBoot 的快速使用,直接采用官方提供的程序代码示例进行。
1、使用 SpringBoot 进行开发,一定要使用 Maven 或者这类项目管理工具完成。
尽管 SpringBoot 以 WEB 程序运行为主,但是在进行创建的时候,Maven 类别直接选择 quickstart 即可,不用理会是否是 WEB 类别。
2、使用 SpringBoot 程序,只需要配置一个官方提供的父 pom 文件即可,再引用 web 包支持。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version></parent>
复制代码
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
复制代码
3、编写程序类,以官网提供的示例说明。
package hello;import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@Controller@EnableAutoConfigurationpublic class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); }}
复制代码
如果 eclipse 安装了 STS 开发插件,支持 SpringBoot 运行模式,则可以直接运行 SpringBoot 代码。也可直接使用 Java Application 运行程序。
没有安装插件的情况下,因为项目基于 Maven,故可以在构建 build 之时 Goals 输入:“spring-boot:run”。这里需要注意,使用 spring-boot:run 指令,是基于 spring-boot 导入的 parent 包支持的。
但是这里需要提醒一下,使用 SpringBoot,对于开发者的要求比较高,必须对 Maven、SSM 开发非常熟悉。
评论