Spring Boot 入门

一、Spring Boot介绍

1.1 Spring Boot简单介绍

Spring Boot是Spring团队在2014年推出的全新框架(不是全新的MVC框架),主要用于简化Spring项目的开发过
程,可以使用少量的代码快速创建Spring项目;

1.2 Spring Boot版本说明

  1. 截至到2021.11.15,官网最新版本:2.5.6,2.6.0.RC1(候选版本)
  2. Spring Boot 2.4.x 之前,正式版本号使用 .RELEASE 结尾,例如: 2.3.12.RELEASE
  3. Spring Boot 2.4.0 开始,正式版本号删除 .RELEASE 后缀,例如: 2.4.11 、 2.5.5
  4. 我后面使用的都是 2.4.7

1.3 Spring Boot的功能、特点

  1. 可以创建独立的Spring应用程序;
  2. 内置了Servlet容器,如Tomcat、Jetty和Undertow(不需要部署war包);
  3. 提供了starter依赖,用于简化构建配置;
  4. 可以按需配置Spring和第三方框架;
  5. 提供了生产级别的功能,例如监控、健康检查和外部化配置;
  6. 没有代码生成、不需要XML配置;

二、开发Spring Boot入门程序

  1. JDK 1.8 +
  2. Maven 3.2 + 、Gradle 4.0 +
  3. 开发工具
    STS:Spring对Eclipse二次封装的工具
    IDEA:java编程语言开发的集成环境

2.1 打开IDEA新建一个Maven项目,不要勾选任何东西

在这里插入图片描述

2.2 填好项目相关信息

在这里插入图片描述

2,3 导入springboot父依赖,springboot-web包,打包插件

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.cyl</groupId>
    <artifactId>springboot-01</artifactId>
    <version>1.0-SNAPSHOT</version>

	<!--导入springboot父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.7</version>
    </parent>

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

    <build>
        <!--打包的名字-->
        <finalName>springboot01</finalName>
        <plugins>
            <!--Spring Boot提供的,可以将项目打成可执行的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.4 新建启动类,名字任意

src/main/java/com/cyl/BootApp.java

package com.cyl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

//启动Spring Boot自动配置
@EnableAutoConfiguration
//根据定义的扫描路径,把符合扫描规则的类装配到spring容器中
@ComponentScan(basePackages = "com.cyl")
public class BootApp {
    public static void main(String[] args) {
        //参数为:(本类.class,main方法中的args参数)
        SpringApplication.run(BootApp.class,args);
    }
}

2.5 配置服务器参数(springboot-web包中内置了Tomcat,我们只需要配置参数即可)

在这里插入图片描述

src/main/resources/application.properties

# servlet 容器的端口
server.port=9090
# context path 应用的上下文路径,也可以称为项目路径,是构成url地址的一部分。
server.servlet.context-path=/boot

2.6 编写控制层

src/main/java/com/cyl/controller/HelloController.java

package com.cyl.controller;

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

@RestController
public class HelloController {

    @RequestMapping(value = "/hello",produces = "text/plain;charset=UTF-8")
    public String hello(@RequestParam(required = false,defaultValue = "World")String name){
        return "Hello"+name;
    }
}

2.7 通过启动类中的main函数运行,输入正确url值访问

在这里插入图片描述

2.8 查看控制台打印:Tomcat、端口号9090、应用上下文路径/boot

在这里插入图片描述

三、 Spring Boot替换Servlet容器

3.1 将默认的Tomcat替换成Jetty,在spring-boot-starter-web依赖中排除spring-boot-starter-tomcat,在pom.xml中引入spring-boot-starter-jetty

    <dependencies>
        <!--spring-boot-start-web
            自动配置了Tomcat,Spring MVC、日志框架Logback、Jackson等-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--移除Tomcat的包-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--引入Jetty容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
    </dependencies>

3.2 启动项目,查看控制台输出日志

在这里插入图片描述

四、springboot 日志打印

因为SpringBoot中自带 logback 日志配置,所以我们只需要在 application.properties 中简单配置
指定打印哪个类的日志:logging.level.包名.包名.*.类名=debug
存到哪里:logging.file.name=文件存放位置

# 例如:指定mapper接口,打印sql语句日志
logging.level.com.cyl.mapper=debug
logging.file.name=d:/log/test.log

我们也可以直接在一个类上标注 @Slf4j
在类中通过 log.info(); log.error(); log.warn(); log.debug(); 使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值