从0开始学习SpringBoot-第1天

Spring Boot 官方教程:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

一、SpringBoot的好处

  1. Provide a radically(根本上) faster and widely accessible(易接近的) getting started experience for all Spring development.
  2. Be opinionated out of the box, but get out of the way quickly as requirements start to diverge(分歧) from the defaults.
  3. Provide a range of non-functional(无功能的) features(特色) that are common to large classes of projects (e.g. embedded(嵌入式的) servers, security, metrics(度量标准), health checks, externalized(给…以外形) configuration).
  4. Absolutely(绝对的) no code generation and no requirement for XML configuration.

使用版本:Spring Boot 1.3.3

依赖:
Java 1.6及以上
Apache Maven 3.2 及以上

二、Spring Boot的简单讲解

  1. Maven嵌入方式
    典型的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.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. Gradle嵌入方式
buildscript {
    repositories {
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

jar {
    baseName = 'myproject'
    version =  '0.0.1-SNAPSHOT'
}

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

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

其他构造方式略。请参考官方文档
如:
Installing(安装) the Spring Boot CLI
Manual installation
Installation(安装) with SDKMAN!
OSX Homebrew installation
MacPorts installation等等

三、第一个例子

从0开始学习SpringBoot-第1天_第1张图片

讲解:
@RestController ,用来表明,用户正在使用这段程序,处理即将过来的请求
@EnableAutoConfiguration ,让Spring Boot猜测你可能使用的Jar

<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>SpringBootTest</groupId>
    <artifactId>springBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>BeiGuo</name>
    <description>第一个SpringBoot项目</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
package springBoot;

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

@RestController
@EnableAutoConfiguration
public class Example {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

好了。大工告成!

在Example中 右键 run as -> java application

控制台出现:

浏览器中的效果:
从0开始学习SpringBoot-第1天_第2张图片

你可能感兴趣的:(spring,springboot)