SpringBoot 2.0 系列001

SpringBoot 2.0 系列001 -- 入门介绍以及相关概念

什么是SpringBoot?

项目地址:http://projects.spring.io/spring-boot/

SpringBoot介绍

Spring Boot使开发独立的,产品级别的基于Spring的应用变得非常简单,你只需"just run"。 我们为Spring平台及第三方库提 供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用需要很少的Spring配置。

SpringBoot功能

1:创建独立的spring应用。

2:嵌入Tomcat, Jetty Undertow 而且不需要部署他们。

3:提供的“starters”poms来简化Maven配置

4:尽可能自动配置spring应用。

5:提供生产指标,健壮检查和外部化配置

6:绝对没有代码生成和XML配置要求

利用Maven构建SpringBoot项目

利用Parent方式

我们可以通过继承spring-boot-starter-parent来使用SpringBoot。

  • 默认java编译级别是1.8
  • 编码默认是UTF-8
  • 具体依赖是spring-boot-dependencies.

其中涵盖各类jar版本号,编码等属性配置,依赖配置,开发信息,协议,插件等管理信息。

org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE

不使用Parent方式

如果说我们有自己的parent项目 我们应该怎么使用SpringBoot呢?

org.springframework.boot spring-boot-dependencies 2.0.1.RELEASE pom import
  • 如果你想更改其中某个依赖的版本,可以在spring-boot-dependencies之前复写这个依赖
org.springframework.data spring-data-releasetrain Fowler-SR2 pom import org.springframework.boot spring-boot-dependencies 2.0.1.RELEASE pom import

使用SpringBoot Maven plugin

SB包含了一个maven插件,可以帮你打包成一个可执行的jar文件,使用方式如下

org.springframework.boot spring-boot-maven-plugin

其他方式

除了maven以外还支持ant和Gradle具体参阅 官方文档

Staters

Staters是一组便捷的依赖描述,比如你想使用jpa功能,只需要引入spring-boot-starter-data-jpa 即可。其他组件基本一样,如spring-boot-starter-* 这种形式。

  • 下列是SB支持的Application staters 

  • production staters 

  • technical starters 

第一个SpringBoot

具体参阅官方文档

  • 1、新建maven项目chapter01,结构如下

  • 2、导入SpringBoot配置

4.0.0 chapter01 chapter01 jar chapter01 Maven Webapp http://www.example.com org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE

      <!--添加额外依赖 -->
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
  
      <!-- 打包 -->
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
  </project>
  • 3、配置启动类如下

package com.ricky;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;

//开启SpringBoot自动注入配置 等价于原来的SpringBootApplication @EnableAutoConfiguration //开启RestController注解 含有ResponseBody 即非页面形式 @RestController public class SpringBootApplication {

@GetMapping("/")
public String home() {
    return "Hello World!";
}

/\*\*
 \* 开启SpringBoot服务
 \* @param args
 \*/
public static void main(String\[\] args) {
    //等价于 new SpringApplication(SpringBootApplication.class).run(args);
    SpringApplication.run(SpringBootApplication.class,args);
}

}

SpringBoot2.0注意事项

  • 容器相关

    • tomcat>=5.5
    • jetty>=9.4
    • Undertow >=1.4
    • servletversion>=3.1
  • jdk

java8或者9

  • Spring框架

5.0.5.RELEASE或者更高

最后

  • 作者ricky
  • 交流群:244930845

评论

暂无评论