• SpringBoot 01 介绍


    相关文档 http://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/

    1、SpringBoot介绍

    SpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework 同属于spring的产品:

    2、SpringBoot好处

    在学习SSM过程中,为了完成框架对接,要进行大量的配置,导入很多的包,如果包之间版本出现不兼容,包之间的依赖管理很麻烦,配置过程占用了过度的时间。

    SpringBoot的设计就是为了解决这个问题的。

    SpringBoot是搭建程序的脚手架,能够帮助我们导入很多的相关包。要使用相关组件只需要导入对应的starter脚手架即可。很多以前在xml配置内容通过写代码的方式实现。

    3、SpringBoot特点

    • 为所有 Spring 的开发者提供一个非常快速的、广泛接受的入门体验
    • 开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),但通过自己设置参数(.properties),即可快速摆脱这种方式。
    • 提供了一些大型项目中常见的非功能性特性,如内嵌服务器、安全、指标,健康检测、外部化配置等
    • 绝对没有代码生成,也无需 XML 配置。

    4、SpringBoot搭建Web项目体验

    1)在磁盘上创建SpingBoot目录

    2)通过Idea打开该目录

    3)创建Maben类型的模块

    4)pom.xml中配置JDK版本

        <!--设置JDK版本-->
        <properties>
            <java.version>1.8</java.version>
        </properties>

    5)pom.xml中添加父工程坐标

        <!--
        添加SpringBoot的基础工程为父工程,这个项目会把包之间的依赖关系处理好,不需要我们处理了
        -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.5.RELEASE</version>
        </parent>

    SpringBoot提供了一个名为spring-boot-starter-parent的工程,内部包含了常用的依赖包(并非全部),我们的Maven项目要以这个项目为父工程,这样我们就不用操心依赖的版本问题。

    6)pom.xml中引入Web启动器包

        <dependencies>
            <!--SpringMVC Web的启动器,会自动引入很多的Maven包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    

    这个依赖添加后,Maven加载结束后,会发现项目中引入了很多的包。

    7)创建启动类

    package rui;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /*启动类*/
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    springBoot的程序启动需要通过该启动类,虽然是Web项目,无需通过Tomcat运行,其内部内置了Tomcat的运行环境,直接运行main函数即可。

    8)编写Controller

    package rui.web;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "hello")
    public class HelloController {
    
        @RequestMapping(value = "index")
        public String hello()
        {
            return "hello, spring boot!";
        }
    }
    

    9)添加yaml配置文件,是一直新的配置文件格式,替代了以前的application.properties,当然application.properties也是可以继续使用的。application.properties的优先级高。

    10)运行程序并测试

     

     

    5、SpringBoot热部署

    1)pom.xml加入依赖包

            <!--热启动的包-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
    

    2)pom.xml增加build配置

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    

     3)application.yaml文件配置

    #热部署
    spring:
      devtools:
        restart:
          enabled: true
    

    4)Idea配置

     

     

     修改文件保存后,控制台会自动输入如下的内容,类似自动启动了一下。

  • 相关阅读:
    squid节点添加新域名测试
    策略路由
    nagios note
    squid日志时间转换
    kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
    kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
    kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
    kuangbin专题十六 KMP&&扩展KMP POJ2752 Seek the Name, Seek the Fame
    kuangbin专题十六 KMP&&扩展KMP POJ2406 Power Strings
    kuangbin专题十六 KMP&&扩展KMP HDU1358 Period
  • 原文地址:https://www.cnblogs.com/feihusurfer/p/15902005.html
Copyright © 2020-2023  润新知