spring boot hello world

spring boot hello world

概念

spring boot 是一个框架,能够让使用者更快速 更少配置地建立一个可用的 可发布的spring应用.

安装

spring boot安装依赖
1.jdk 1.8或以上版本
2.这里最佳实践是通过spring.io创建一个spring maven 项目 with spring boot
https://start.spring.io/

直接将生成好的project文件解压后,已经是创建好的maven project了.
./mvnw verify 之后会本地下载和安装一个maven根据网络情况可能要十几分钟

2.pom.xml已经添加好了spring boot的相关依赖

<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.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</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>
    </plugins>
</build>
</project>

这里定义了这个pom文件的父类是spring-boot-starter-parent,这是一个比较推荐的方式,当然也可以继承别的父类,然后通过import的方式使用spring boot,官网有介绍

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

spring boot maven 插件支持以jar 包方式发布应用

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

spring boot 和test 依赖

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

3.src/main/java/com/example/demo 下会有一个 DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    DemoApplication(){

           System.out.println("hello world!");
    }

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

然后执行 ./mvnw package 将会下载相关依赖然后打包,target下有发布包
demo-0.0.1-SNAPSHOT.jar.original是原始应该大小,demo-0.0.1-SNAPSHOT.jar增加了相关依赖要大些
java -jar demo-0.0.1-SNAPSHOT.jar //可以运行应用,打印 hello world!

4.我们还可以建立一个web应用版本的hello world

package com.example.demo
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

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

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

}

pom.xml需要增加web依赖

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

mvnw package之后,java -jar /Users/hbjava1985/Downloads/demo/target/demo-0.0.1-SNAPSHOT.jar 之后 浏览器访问
http://localhost:8080/ 就会出现hello world!

5.为了更方便地使用spring boot,可以去安装 spring boot cli,这个工具可以使用命令行的方式让你使用spring boot
下载之后解压 https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.0.1.RELEASE/spring-boot-cli-2.0.1.RELEASE-bin.zip

bin/spring //执行cli

写一个测试 app.groovy

@RestController
class ThisWillActuallyRun {

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

}

bin/spring run app.groovy
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页