首页 > 技术文章 > 初识SpringBoot(IDEA)

hanlin-hl 2020-11-07 23:18 原文

1. 环境准备

  • JDK1.8
  • maven 3.3 以上版本
  • IDEA.2018
  • SpringBoot 1.5.9.RELEASE

2. 创建简单的maven工程

3. 在pom.xml 导入SpringBoot相关的依赖

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

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

4. 编写SpringBoot入口程序

package com.atguigu;

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

/**
 * @SpringBootApplication 标注主程序类,说明是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {

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

5. 编写Controller

package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello SpringBoot !";
    }
}

6. 运行MainApplication

7. 打包Jar包运行

7.1 在pom.xml导入插件

    <!-- 该插件,可以将应用程序打包成一个可执行的jar包 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

7.2 打包

  • 可以看到控制台已经将打包好的 jar 包放在了 target 目录下

7.3 我们可以将 jar 包复制到 D:盘根目录下

  • 将 打包好的 jar 包复制到目录下之后,打开命令行窗口输入

    java -jar springboot_01-1.0-SNAPSHOT.jar
    
  • 可以看到 SpringBoot 应用程序启动

  • 在浏览器输入: http://localhost:8080/hello 同样可以看到

推荐阅读