SpringBoot简介

来源:互联网 发布:如何运行php文件 编辑:程序博客网 时间:2024/06/06 07:52

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

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

产品特点

  • 创建可以独立运行的Spring 应用。
  • 直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件。
  • 提供固定的’starter’ POMs来简化你的Maven配置。
  • 尽可能自动配置Spring。
  • 提供准产品特性,如性能指标、健康检查和外部化配置。
  • 没有代码生成,也没有 XML 配置要求。
  • 参考指南包括对所有特性的详细描述,以及对常用用例的详细说明。

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.8repositories {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.8repositories {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@EnableAutoConfigurationpublic 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/可以查看运行效果。

原创粉丝点击