Spring Boot 教程 - HelloWorld

1. Spring Boot 的由来

你们都知道,Spring框架是Java生态中举足轻重的轻量型框架,帮助咱们广大的大佬们进行Java开发。Spring框架的发展很是的迅速,从最开始的Spring核心容器帮助咱们管理Java对象,到后来各类业务上的问题,Spring框架几乎都有与之对应的解决方案。但随着愈来愈多的业务集成到Spring框架中,Spring与各组件的配置文件愈来愈多,配置愈来愈繁杂,开发人员不能很好的专一业务逻辑,给开发人员带来了困扰。因此说,Spring框架自己是轻量型的,可是Spring的配置倒是重量型的。Spring的开发者早早的就注意到了这个问题,为了解决这个问题,Spring Boot 应运而生。Spring Boot 的核心理念是约定优于配置。即:Java开发者不须要关心各个jar包之间的依赖关系,依赖关系由Spring开发者们提早帮大家配置好了,并打成jar包,Java开发者只须要引入jar就能够快速开发,极大的提升了Java开发者的效率,并且配置文件也只剩下了一个。在Spring Boot 出现以前,开发一个项目的的项目搭建工做可能须要30分钟左右,Spring Boot 出现以后,5分钟都不要,开发者们就能够进行代码的编写。极大的提升了开发者的效率,也为Spring 框架注入了新的生命力。java

2. Spring Boot 的 hello world

Spring Boot 这么好的框架咱们怎么能不学呢?接下来,我将带领你们学习 Spring Boot 这个开启新纪元的框架,学习一个新知识,确定是从 hello world 开始啦!git

  • 2.1 建立工程,引入依赖github

    建立工程相信你们都会,省略此过程,直接引入maven依赖,pom.xmlweb

    <!--springboot父工程-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <dependencies>
            <!--springboot框架web组件-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.2.2.RELEASE</version>
            </dependency>
        </dependencies>
    
        <build>
            <!--springboot的maven插件-->
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
            </plugins>
        </build>
  • 2.2 配置文件的配置spring

    application.ymlapache

    server:
      port: 8080 # 应用的端口
      servlet:
        context-path: /butterflytri # 整个应用的映射地址
    spring:
      application:
        name: helloworld # 应用名称
  • 2.3 代码编写springboot

    • 首先建立包:app

    • 建立HelloWorldApplication.java类:框架

      package org.butterflytri;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      /**
       * @author: WJF
       * @date: 2020/5/15
       * @description: HelloWorldApplication
       */
      
      /**
       * {@link SpringBootApplication}: 这个注解帮助你加载配置文件{@link 'application.yml'}的配置,
       *                                被这个注解标识的类就是启动类,是整个工程的入口。
       */
      @SpringBootApplication
      public class HelloWorldApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(HelloWorldApplication.class,args);
          }
      
      }
    • 建立HelloWorldController.java类:maven

      package org.butterflytri.controller;
      
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * @author: WJF
       * @date: 2020/5/15
       * @description: Hello World!
       */
      @RestController
      @RequestMapping("/helloWorld")
      public class HelloWorldController {
      
          private static final String HELLO_WORLD = "Hello World!";
      
      
          /**
           * 运行项目,访问:http://localhost:8080/butterflytri/helloWorld/go 便可看到hello world!
           * @return String
           */
          @RequestMapping("/go")
          public String go() {
              return HELLO_WORLD;
          }
      
      }
  • 2.4 启动工程,访问方法

    访问http://localhost:8080/butterflytri/helloWorld/go路径,便可在页面看到应用的响应结果:Hello World!

3. 项目地址

本项目传送门:spring-boot-helloworld

此教程会一直更新下去,以为博主写的能够的话,关注一下,也能够更方便下次来学习。

相关文章
相关标签/搜索