美文网首页Java
SpringBoot | SpringBoot入门介绍及其分析H

SpringBoot | SpringBoot入门介绍及其分析H

作者: 一颗白菜_ | 来源:发表于2019-12-21 19:43 被阅读0次

    一、SpringBoot介绍

    1、SpringBoot简介

    简化Spring应用开发的一个框架
    整个Spring技术栈的一个大整合
    J2EE开发的一站式解决方案

    2、微服务

    微服务是一种架构风格,一个应用应该是一组小型服务,可以通过HTTP的方式进行互通。

    每一个功能元素最终都是一个可独立替代和独立升级的软件单元。

    与之相反的是单体应用,所有功能应用都集成为一体。


    二、使用maven创建SpringBoot项目

    1、创建一个maven工程

    2、导入Springboot依赖

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    

    3、编写一个主程序:启动SpringBoot

    package com.cerr;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    //@SpringBootApplication标注一个主程序类,说明这是一个Springboot应用
    @SpringBootApplication
    public class HelloWorldMainApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldMainApplication.class,args);
        }
    }
    

    4、编写Controller

    package com.cerr.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class HelloWorld {
        @ResponseBody
        @RequestMapping("/hello")
        public String hello(){
            return "hello world";
        }
    }
    
    

    5、启动方式

    有两种启动的方式:

    • 直接运行启动类:运行有标注@SpringBootApplication的类
    • 打包部署后,通过命令行来启动

    下面我们来讲解第二种方式:

    6、简化部署

    导入maven插件

        <!--可以将应用打包成一个可执行的jar包 -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

    然后就可以点击下图的package将这个应用打包成jar包,直接使用java -jar的命令进行执行:



    在终端中使用java -jar命令启动:


    在终端中启动
    访问:

    三、对上述项目的探究

    1、POM文件

    父项目:

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

    它的父项目来真正管理SpringBoot应用的所有依赖版本:

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath>../../spring-boot-dependencies</relativePath>
        </parent>
    

    所以以后我们导入依赖默认是不需要写版本的(没有在dependencirs里面管理的依赖就需要写版本)

    启动器:

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

    spring-boot-starter-web是Springboot的场景启动器,在其中帮我们导入了web模块正常运行所依赖的组件

    SpringBoot将所有的功能场景都抽取出来,做成一个个的startes(启动器),只需要在项目里面引入这些starters,相关场景的所有依赖都会导入进来。

    2、主程序类(主入口类)

    /**
     *  @SpringBootApplication标注一个主程序类,说明这是一个Springboot应用
     */
    @SpringBootApplication
    public class HelloWorldMainApplication {
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldMainApplication.class,args);
        }
    }
    

    @SpringBootApplication:标注在某个类上,说明这个类是Springboot的主配置类,Springboot就应该运行这个类的main方法来启动Springboot应用。

    我们来查看@SpringBootApplication这个注解,发现其是一个组合注解:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
        excludeFilters = {@Filter(
        type = FilterType.CUSTOM,
        classes = {TypeExcludeFilter.class}
    ), @Filter(
        type = FilterType.CUSTOM,
        classes = {AutoConfigurationExcludeFilter.class}
    )}
    )
    
    • @SpringBootConfiguration:Springboot的配置类,标注在某个类上,表示这是一个SpringBoot的配置类。该Springboot的配置类里面使用了@Configuration注解,这个注解是注解在配置类(作用等同于配置文件)上面的,配置类也是容器中的一个组件。
    • @EnableAutoConfiguration:开启自动配置功能。以前我们需要配置的东西,使用了这个注解之后,Springboot都会帮我们自动配置。其里面的注解如下:
    @AutoConfigurationPackage
    @Import({EnableAutoConfigurationImportSelector.class})
    

    @AutoConfigurationPackage是自动配置包的注解,其作用本质上是将主配置类(@SpringBootApplication标注的类)的所在包及其下面所有子包的所有组件扫描到Spring容器中。
    @Import({EnableAutoConfigurationImportSelector.class})将所有需要导入的组件导入进容器中,会给容器导入很多的自动配置类,即给容器导入这个场景需要的所有组件,并配置好这些组件。
    有了这些配置类,我们就不需要手动编写配置注入功能组件。

    对于JavaEE整体的解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar中。


    四、使用Spring Initializr来快速创建一个SpringBoot项目

    SpringBoot | 使用Spring Initializr来快速创建一个SpringBoot项目

    默认生成的SpringBoot项目:

    • 主程序已经写好了,我们只需要编写我们自己的逻辑代码

    • resources文件夹中的目录结构:

      • static:保存所有的静态资源:js css等等
      • templates:保存所有的模板页面(SpringBoot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面),可以使用模板引擎(freemarker,thymeleaf等)
      • application.properties:Springboot应用的配置文件,可以修改一些默认设置。

    相关文章

      网友评论

        本文标题:SpringBoot | SpringBoot入门介绍及其分析H

        本文链接:https://www.haomeiwen.com/subject/czypnctx.html