SpringBoot学习!!!

Spring Boot学习笔记

一.什么是SpringBoot

SpringBoot是一种全新的Spring框架,设计目的是用来简化Spring应用的创建和开发过程

二.为什么要用SpringBoot

特性

  • 能够更加快速的创建基于Spring的应用程序
  • 不需要部署war包,直接使用内嵌的Tomcat来运行程序
  • 自动化配置Spring、Spring mvc等
  • 简化Maven的配置
  • 提供了代码健康检查等功能
  • 不用生成代码,也不用XML配置

核心功能

  • 自动配置

    针对基于Spring的应用程序,能自动提供相关配置

  • 起步依赖

    告诉SpringBoot需要什么功能,就能引入需要的依赖库

三.HelloWord!

1. 创建一个Maven工程

在这里插入图片描述

2. 选择名称和路径

在这里插入图片描述

3. 导入SpringBoot相关依赖

3.1 打开SpringBoot官网手册
在这里插入图片描述
代码如下

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

3.2 idea中的操作

在这里插入图片描述

3.3 导入依赖
同样是在SpringBoot官方手册
在这里插入图片描述

代码如下

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

3.4 idea中的操作
在这里插入图片描述

4. 创建程序

4.2 创建一个类文件
在这里插入图片描述

4.2.1 输入名称
在这里插入图片描述

名称的格式
建议是com.example.myproject

com 
example
    +- myproject
      +- Application.java
      |
      +- model
      |  +- Customer.java
      |  +- CustomerRepository.java
      |
      +- service
      |  +- CustomerService.java
      |
      +- controller
      |  +- CustomerController.java
      |
  • Application.java 建议放到根目录下面,主要用于做一些框架配置
  • model 目录主要用于实体与数据访问层(Repository)
  • service 层主要是业务类代码
  • controller 负责页面访问控制

5. 编写主程序

package com.alan.boot;


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

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

6. 编写HelloWorld程序

6.1 创建一个类文件
在这里插入图片描述
6.2 输入程序名称

在这里插入图片描述

6.3 编写代码

package com.alan.boot.controller;


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

@RestController
public class HelloWorld {

    @RequestMapping("hello")
    public String print() {
        return "HelloWorld! SpringBoot is fun!";
    }
}

7. 运行程序

点击运行控制台会出现一下内容
在这里插入图片描述

Tomcat started on port(s): 8080 (http) with context path ‘’
说明已经在8080端口启动了

8. 测试

在浏览器中输入locallhost:8080/hello
在这里插入图片描述
到这里一个HelloWorld程序就成功了

三. HelloWorld源码分析

1.启动器

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

spring-boot-starter-web:帮我们导入web模块正常运行所依赖的组件

spring boot 将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目里引入这些starter相关场景的所有依赖都会被导入进来,要用什么功能就导入什么场景的启动器

2.@SpringBootApplication

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

@SpringBootApplication:
说明这个类为SpringBoot的主配置类,告诉SpringBoot这是 个SpringBoot应用程序,SpringBoot的运行就应该在这个main方法中来运行

3.@RequestMapping

@RequestMapping("/hello")
    public String print() {
        return "HelloWorld! SpringBoot is fun!";
    }

@RequestMapping(“/hello”):
映射请求,浏览器向用户发送"hello"请求,然后处理,再向浏览器返回"HelloWorld!Spring is fun!"字符串

4.@RestController

进入RestController注解中

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.web.bind.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Controller;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

可以看到其继承了Controller接口和RestController接口,使代码变得简洁不需要一个个加注释

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值