Spring Boot 简单入门

Spring Boot 简单入门

Spring Boot 入门简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置

  • 轻松创建独立的Spring应用程序。
  • 内嵌Tomcat、jetty等web容器,不需要部署WAR文件。
  • 提供一系列的“starter” 来简化的Maven配置。
  • 开箱即用,尽可能自动配置Spring。

Spring Boot构建

开发环境

  • IntelliJ IDEA 2018.1.6 x64
  • java 1.8
  • apache-maven-3.5.4-bin

在线构建Spring Boot

在开始构建之前推荐一个在线构建Spring Boot的网址 Spring Initializr
在这里插入图片描述
这个网站里面可以在线进行Spring Boot构建。
可以选择创建Spring Boot的方式、创建的语言、Spring Boot的版本、需要的jar包等,输入完必要的参数之后点击 Generate - Ctrl + ⏎ 就会下载项目的压缩包,之后可以把压缩包解压可以直接导入到 IDEA 或者是 Eclipse 当中

本地构建Spring Boot项目

本地构建Spring Boot使用的工具是IDEA 创建Maven项目,当然工具都是一样的,只要用着顺手就行之后大部分的操作就使用图片来代替语言了

  • 打开IDEA之后,创建工程,选择Maven项目,选中 Create from archetype 在下面找到 org.apache.maven.archetype :maven-archetype -webapp选中,点击下一步

createSpringBoot-1

  • 输入 GroupId 和 ArtifactId ,之后直接点击下一步
    createSpringBoot-2

  • 输入项目名和项目地址,然后点击Finish 完成项目的创建
    createSpringBoot-3

  • 等待项目构建完成,之后完成项目,构建之后的项目的目录为
    createSpringBoot-4

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>springboot</groupId>
    <artifactId>springbootTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>springbootTest Maven Webapp</name>
    <url>http://www.example.com</url>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.5.10.RELEASE</version>
        </dependency>
    </dependencies>

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

AppApplication.java

package com.text;

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

@SpringBootApplication
public class AppApplication {

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

TestController.java

package com.text;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/hello")
    public String HelloWorld() {
        return "HelloWorld";
    }
}

运行项目,出现如下内容,则说明启动成功

  • 访问项目 http://localhost:8080/hello
    createSpringBoot-5
    启动的方式有两种
    • 通过AppApplication类的main方法启动
    • 通过maven启动项目(必须引入spring-boot-maven-plugin插件),在maven项目中运行 spring-boot:run
      createSpringBoot-6
      至此,项目大功告成

Spring Boot很容易的创建一个独一的运行(运行jar、内置Servlet容器),准生产级别基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很少的Spring配置

更换显示的Bunner

代码实现:

  • 在 resources 中加入 banner.txt 文件
    banner

  • banner.txt

    ${AnsiColor.BRIGHT_YELLOW}
    ===================================================================================
    
    
    
    //                          _ooOoo_                               //
    //                         o8888888o                              //
    //                         88" . "88                              //
    //                         (| ^_^ |)                              //
    //                         O\  =  /O                              //
    //                      ____/`---'\____                           //
    //                    .'  \\|     |//  `.                         //
    //                   /  \\|||  :  |||//  \                        //
    //                  /  _||||| -:- |||||-  \                       //
    //                  |   | \\\  -  /// |   |                       //
    //                  | \_|  ''\---/''  |   |                       //
    //                  \  .-\__  `-`  ___/-. /                       //
    //                ___`. .'  /--.--\  `. . ___                     //
    //              ."" '<  `.___\_<|>_/___.'  >'"".                  //
    //            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
    //            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
    //      ========`-.____`-.___\_____/___.-`____.-'========         //
    //                           `=---='                              //
    //      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
    //            佛祖保佑       永不宕机     永无BUG                   //
    
    
    
    :: Spring Boot :: ${spring-boot.version}

运行即可在控制台看到,打印的图案

取消显示的Bunner

在AppApplication文件中使用SpringApplication 关闭Banner

  • AppApplication.java
    @SpringBootApplication
    public class AppApplication {
    
        public static void main(String[] args) {
    
            // SpringApplication.run(AppApplication.class, args);
            SpringApplication application = new SpringApplication(AppApplication.class);
            //关闭bunner
            application.setBannerMode(Banner.Mode.OFF);
            application.run(args);
        }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值