SpringBoot简介

2017-07-03  本文已影响110人  勇赴

采用固定的形式构建准产品的Spring应用程序。Spring Boot支持约定优于配置,旨在让你尽快地启动并运行Spring应用程序。

SpringBoot很容易创建基于Spring的独立的、产品级的,可以“即时运行”的应用程序。我们对Spring平台和第三方库采用固定的形式,这样你就可以以最小的配置开始使用。大多数SpringBoot应用程序只需要很少的Spring配置。

产品特点

Quick Start

以eclipse为例,需要安装STS来支持Spring Boot的开发,安装gradle或maven来管理Spring Boot程序的依赖项和构建它们。

可以从https://start.spring.io/ 这个地址可以下载一个空的Spring Boot项目。以gradle为例,导入到STS之后的项目结构如下:

项目结构.png

在项目中打开build.gradle文件。

buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

可将mavenCentral()替换为maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'} ,这样的话就可以用阿里云的maven仓库了。下载第三方依赖的时候会很快。

在dependencies下添加compile("org.springframework.boot:spring-boot-starter-web:2.0.0.M2")就可以开发spring mvc程序了。

更改如果的build.gradle文件:

dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

只需在项目中添加SampleController 类。

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

然后Run As -> Spring Boot App就可以运动了。打开localhost:8080/可以查看运行效果。

上一篇 下一篇

猜你喜欢

热点阅读