栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot学习!!!

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot学习!!!

Spring Boot学习笔记

文章目录
  • Spring Boot学习笔记
    • 一.什么是SpringBoot
    • 二.为什么要用SpringBoot
      • 特性
      • 核心功能
    • 三.HelloWord!
      • 1. 创建一个Maven工程
      • 2. 选择名称和路径
      • 3. 导入SpringBoot相关依赖
      • 4. 创建程序
      • 5. 编写主程序
      • 6. 编写HelloWorld程序
      • 7. 运行程序
      • 8. 测试
    • 三. HelloWorld源码分析
      • 1.启动器
      • 2.@SpringBootApplication
      • 3.@RequestMapping
      • 4.@RestController

一.什么是SpringBoot

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

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

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

  • 起步依赖

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

三.HelloWord! 1. 创建一个Maven工程

2. 选择名称和路径

3. 导入SpringBoot相关依赖

3.1 打开SpringBoot官网手册

代码如下


        org.springframework.boot
        spring-boot-starter-parent
        2.7.0
    

3.2 idea中的操作

3.3 导入依赖
同样是在SpringBoot官方手册中

代码如下


    
        org.springframework.boot
        spring-boot-starter-web
    

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.启动器

    org.springframework.boot
    spring-boot-starter-web

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接口,使代码变得简洁不需要一个个加注释

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985967.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号