Spring Boot 实践折腾记(一):快速,Hello World例子

239 阅读3分钟

规划人生旅程是你自己责无旁贷的责任,没有其他人可以替你规划。即便是你的老板,也没有这个义务。要实现自我发展,必须同时做好两件事:精益求精和尝试新鲜。——《非营利性组织的管理》

开始前

最近闲下来,看了些Spring Boot和Docker的相关资料。

说实话,Spring Boot官网的demo和文档写得真是"简洁",看起好像是那么回事,挺好懂的,但自己实践后才知道是坑坑不少,而且boot的文档较少,相比而言,docker的文档一搜一大把,系列里不会过多的介绍docker,主要是穿插着使用。

于是,想着写个简单的Spring Boot学习与使用系列,以Spring Boot为切入点,总结下折腾Spring Boot和docker的结果。

Spring Boot项目的目的就是为了简化常用的配置,提升效率,而提出的一种新的解决思路,让笨重的java,也可以像js和python一样,快速的应用部署。快速、快速、还是快速。

系列开篇,想着直接开始工程搭建上代码吧,怕太枯燥了,所以,写了以上这些废话,接下来开始,实际操作。

Spring Boot有哪些核心功能

  1. 基于Spring框架的独立运行的项目
  2. 内嵌Servlet容器
  3. 提供starter简化Maven配置
  4. 自动配置Spring
  5. 无xml配置和简化代码配置

Spring Boot版本

1.3.5.RELEASE   

使用的是最新的稳定发布版。

IDE

IntelliJ IDEA 15.0.5

推荐使用idea,如果喜欢用eclipse的,示例并不影响,只是可能需要自行配置一些环境设置。

构建Maven项目

虽说可以使用idea建立spring boot项目,但为了方便还是使用maven来建工程。

建立父项目

为了后续多个demo演示方便,这里建立一个多个子项目的maven父工程,并引入一次boot-starter-web,这样后续子项目中不需要再添加boot-starter-web依赖。

//pom.xml

<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
	<module>boot-start</module>
</modules>

<properties>
	<spring.boot.version>1.3.5.RELEASE</spring.boot.version
</properties>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<version>${spring.boot.version}</version>
</dependency>

这里先不添加编译插件,后续在不同的应用模块里再分别添加。

建立子项目

包名:com.hjf.boot.demo.boot_start

在pom.xml中增加编译项

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Hello World

com.hjf.boot.demo.boot_start.StartApp.Class

为了自动配置的方便,按照Spring Boot约定规则,在最外层的目录添加启动类StartApp

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by hjf on 2016/6/7.
 * com.hjf.boot.demo.boot_start
 */
@EnableAutoConfiguration  //1
@ComponentScan  //2
@RestController //3
public class StartApp {
    public static void main(String[] args) {
        SpringApplication.run(StartApp.class,args); //4
    }

    @RequestMapping("/hello")  //5
    public String hello(){
        return "Hello world!";
    }
}

说明:

  1. 开启自动配置
  2. 开启bean扫描
  3. spring mvc的rest控制器方法
  4. boot启动的核心方法,使用SpringApplication对象
  5. 提供一个访问接口"/hello",来展示数据。

启动

点击Run --> Edit Configuration,编辑Spring Boot启动参数,如下图:

这里写图片描述

启动应用,查看控制台打印,默认端口是8080

这里写图片描述
这里写图片描述

访问:http://localhost:8080/hello 查看数据
这里写图片描述
到此,本章内容结束。

小结

本章只是引入Spring Boot一个最简单的例子,说明Spring Boot启动的快速,不需要配置文件,不需要按安装tomcat,只需要一个class文件,即可启动基于Spring MVC的应用。

github示例地址:github.com/mickjoust10…