SpringBoot之Helloworld

来源:互联网 发布:淘宝多少天自动付款 编辑:程序博客网 时间:2024/06/10 12:04

1.SpringBoot简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

2.SpringBoot特性

  1. 创建独立的Spring应用程序
  2. 嵌入的Tomcat,无需部署WAR文件
  3. 简化Maven配置
  4. 自动配置Spring
  5. 提供生产就绪型功能,如指标,健康检查和外部配置
  6. 绝对没有代码生成和对XML没有要求配置
    以上的这些内容参考百度百科
    简而言之,SpringBoot并不是一个全新的web开发框架,只是给Spring的开发提供了另外一种方式。传统的Spring开发需要大量的模板文件和xml配置文件,尤其是配置文件,非常繁琐。而使用SpringBoot,就可以免去大量的配置文件。除此之外,SpringBoot默认提供了一些默认的依赖,所以 创建一个web工程,不需要再写大量的Maven依赖,极大的简化了开发流程。
    SpringBoot的功能很多,我目前感觉最实用的两大特性就是no xml和提供默认的依赖。极大地简化了开发

3.项目结构

SpringBoot项目一般都是一个Maven项目。项目结构图如下:
这里写图片描述
因为只是一个Helloworld工程,因此项目结构很简单,没有涉及任何web的配置文件。
HelloSpringBoot是一个入口类,也是一个控制类。pom.xml是Maven的配置文件。

4 pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><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>com.dudu</groupId>    <artifactId>chapter1</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>chapter1</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.1.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>        </plugins>    </build></project>

4.1 Spring Boot父级依赖

    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.1.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>

这块配置就是Spring Boot父级依赖,有了这个,当前的项目就是Spring Boot项目了,spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖,使用它之后,常用的包依赖可以省去version标签。

4.2 起步依赖

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>

有两个依赖,分别是spring-boot-starter-webspring-boot-starter-test
以spring-boot-starter-web为例,这个依赖模块几乎包括了所有的web开发必用的依赖模块,比如:
这里写图片描述
上面这幅图是直接从网上下载的。
简而言之,起步依赖提供了一些默认的依赖。只要引入web起步依赖,几乎常用的web依赖都会被包括进来,从而省去了繁琐的maven配置。

5.应用入口类

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@SpringBootApplicationpublic class HelloSpringBoot {    @RequestMapping("/hello")    public String index(){        return "Hello Spring Boot";    }    public static void main(String[] args) {        SpringApplication.run(HelloSpringBoot.class, args);    }}
  1. @RestController并不是SpringBoot特有的注解,等同于@Controller+@ResponseBody的结合,使用这个注解的类里面的方法都以json格式输出。
  2. @SpringBootApplication是Sprnig Boot项目的核心注解,主要目的是开启自动配置。
  3. @RequestMapping(“/hello”)和SpringMVC中的注解作用一样,也是起到请求路径映射的作用。
  4. 必须有一个main方法,main方法里面的代码几乎不用动。主要作用是作为项目启动的入口。

SpringBoot项目启动的方式有很多种,其中最简单的方式就是直接启动main方法。
控制台输出的内容基本如下:

  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.5.1.RELEASE)

在浏览器中输入:http://localhost:8080/hello
浏览器显示的数据如下:
Hello Spring Boot:

参考的博客:http://tengj.top/2017/02/26/springboot1/
作者:嘟嘟独立博客

原创粉丝点击