zoukankan      html  css  js  c++  java
  • Spring Boot快速入门

    一、环境搭建

    1. 创建Maven工程

    使用idea工具创建一个maven工程,该工程为普通的java工程即可

    2. 添加Spring Boot的起步依赖

    Spring Boot要求项目要继承Spring Boot的起步依赖spring-boot-starter-parent

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

    Spring Boot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

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

    3. 编写Spring Boot引导类

    要通过Spring Boot提供的引导类启动Spring Boot才可以进行访问,启动过程中Spring Boot会默认扫描和引导类处在同一个包下的所有Bean并注入IoC容器。

    package com.itheima;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MySpringBootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MySpringBootApplication.class);
        }
    
    }
    

    4. 编写Controller

    在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController

    package com.itheima.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class QuickStartController {
        
        @RequestMapping("/quick")
        @ResponseBody
        public String quick(){
            return "hello springboot!!!";
        }
        
    }
    

    5. 测试

    执行SpringBoot起步类的主方法,控制台打印日志如下:

    .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.0.1.RELEASE)
    
    2018-05-08 14:29:59.714  INFO 5672 --- [           main] com.itheima.MySpringBootApplication      : Starting MySpringBootApplication on DESKTOP-RRUNFUH with PID 5672 (C:UsersmuzimooIdeaProjectsIdeaTestspringboot_quick	argetclasses started by muzimoo in C:UsersmuzimooIdeaProjectsIdeaTest)
    ... ... ...
    o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-08 14:30:03.126  INFO 5672 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2018-05-08 14:30:03.196  INFO 5672 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2018-05-08 14:30:03.206  INFO 5672 --- [           main] com.itheima.MySpringBootApplication      : Started MySpringBootApplication in 4.252 seconds (JVM running for 5.583)
    

    通过日志发现,Tomcat started on port(s): 8080 (http) with context path ''

    tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空

    打开浏览器访问url地址为:http://localhost:8080/quick

    二、快速入门解析

    1. Spring Boot代码解析

    • @SpringBootApplication:标注Spring Boot的启动类,该注解具备多种功能(后面详细剖析)
    • SpringApplication.run(MySpringBootApplication.class) 代表运行Spring Boot的启动类,参数为Spring Boot启动类的字节码对象

    2. Spring Boot工程热部署

    ​ 我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,我们可以在修改代码后不重启就能生效,在 pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署。

    <!--热部署配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    

    注意:IDEA进行Spring Boot热部署失败原因

    出现这种情况,并不是热部署配置问题,其根本原因是因为Intellij IEDA默认情况下不会自动编译,需要对IDEA进行自动编译的设置,如下:

    然后 Shift+Ctrl+Alt+/,选择Registry

    三、使用idea快速创建Spring Boot项目

    ​ 通过idea快速创建的Spring Boot项目的pom.xml中已经导入了我们选择的web的起步依赖的坐标,并且已经为我们编写好了引导类。因此,我们只需要编写Controller即可。

  • 相关阅读:
    图像 resize 代码:保留 aspect ratio(长宽比)
    Pytorch lr_scheduler 中的 last_epoch 用法
    torch.optim用法(参数组的设置)
    课程式学习(Curriculum Learning)
    扇贝单词本-隐藏中文释义 tampermonkey
    电话号码正向标记认证网站申请地址
    考研英语做题计时器网页版(每隔3分钟播放声音,提醒计时)
    mac关闭自动更新后还在每天提醒进行安装更新
    mac 自动生成自签证书脚本
    Ditto
  • 原文地址:https://www.cnblogs.com/jiajun107/p/13219707.html
Copyright © 2011-2022 走看看