zoukankan      html  css  js  c++  java
  • 10分钟学会SpringBoot入门

    原有Spring优缺点分析

    优点

    Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品。无需开发重量级的Enterprise JavaBean(EJB),Spring为企业级Java开发提供了一种相对简单的方法,通过依赖注入和面向切面编程,用简单的Java对象(Plain Old Java Object,POJO)实现了EJB的功能。

    缺点

    Spring特性配置

    项目的依赖管理

    SpringBoot解决Spring的缺点

    SpringBoot对上述Spring的缺点进行的改善和优化,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期。

    SpringBoot的特点

    • 为基于Spring的开发提供更快的入门体验
    • 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求
    • 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
    • SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式

    SpringBoot的核心功能

    • 起步依赖

      起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

      简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。

    • 自动配置

      Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是Spring自动完成的。

        注意:起步依赖和自动配置的原理剖析会在《SpringBoot原理分析》进行详细讲解
      

    SpringBoot快速入门

    代码实现

    创建Maven工程

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

    创建成功的目录

    添加SpringBoot的起步依赖

    SpringBoot要求,所有的springboot工程都必须继承spring-boot-starter-parent

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

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

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

    编写SpringBoot引导类

    要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问

    在java目录下创建MySpringBootApplication

    package com.ben.springboot_start.com.ben;
    
    import org.springframework.boot.SpringApplication;
    
    /**
     * @ClassName: MySpringBootApplication
     * @author: benjamin
     * @version: 1.0
     * @description: SpringBoot引导类
     * @createTime: 2019/08/15/16:23
     */
    
    public class MySpringBootApplication {
        public static void main(String[] args) {
    
            SpringApplication.run(MySpringBootApplication.class);
    
        }
    }
    
    

    编写Controller

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

    package com.ben.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    /**
     * @ClassName: QuickStartController
     * @author: benjamin
     * @version: 1.0
     * @description: TODO
     * @createTime: 2019/08/15/16:25
     */
    
    @Controller
    public class QuickStartController {
        
        @RequestMapping("/quick")
        @ResponseBody
        public String quick(){
            return "springboot 访问成功";
        }
    
    }
    
    

    测试

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

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.1.7.RELEASE)
    
    2019-08-15 16:30:52.857  INFO 10516 --- [           main] com.ben.MySpringBootApplication          : Starting MySpringBootApplication on benjie with PID 10516 (D:Javaspringbootdemospringboot_start	argetclasses started by benjie in D:Javaspringbootdemospringboot_start)
    2019-08-15 16:30:52.873  INFO 10516 --- [           main] com.ben.MySpringBootApplication          : No active profile set, falling back to default profiles: default
    2019-08-15 16:30:56.310  INFO 10516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2019-08-15 16:30:56.342  INFO 10516 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2019-08-15 16:30:56.342  INFO 10516 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.22]
    2019-08-15 16:30:56.592  INFO 10516 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2019-08-15 16:30:56.592  INFO 10516 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3625 ms
    2019-08-15 16:30:56.935  INFO 10516 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2019-08-15 16:30:57.310  INFO 10516 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2019-08-15 16:30:57.310  INFO 10516 --- [           main] com.ben.MySpringBootApplication          : Started MySpringBootApplication in 5.077 seconds (JVM running for 5.727)
    2019-08-15 16:31:15.486  INFO 10516 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2019-08-15 16:31:15.487  INFO 10516 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2019-08-15 16:31:15.495  INFO 10516 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms
    

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

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

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

    SpringBoot代码解析

    • @SpringBootApplication:标注SpringBoot的启动类,该注解具备多种功能(后面详细剖析)
    • SpringApplication.run(MySpringBootApplication.class) 代表运行SpringBoot的启动类,参数为SpringBoot启动类的字节码对象
  • 相关阅读:
    LOJ P10004 智力大冲浪 题解
    LOJ P10011 愤怒的牛 题解
    LOJ P10002 喷水装置 题解
    洛谷 P2279 [HNOI2003]消防局的设立 题解
    洛谷 P5640 【CSGRound2】逐梦者的初心 题解
    洛谷 P2827 蚯蚓 题解
    [SHOI2012]魔法树
    浅析树链剖分
    [Bzoj1731]排队布局
    [POJ-1201]Intervals
  • 原文地址:https://www.cnblogs.com/benjieqiang/p/11359084.html
Copyright © 2011-2022 走看看