SpringBoot快速入门

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> SpringBoot快速入门

SpringBoot入门

Author:Alan

公众号:阿风的JAVA

Spirng boot 入门引导部分

(如果您直接书写案例,您可以跳过此部分)

关于Spring Boot 百度百科是这么介绍的:

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

与其它框架不同,SpirngBoot并不能称之为一个框架,而是针对spirng在整合各种技术的配置繁琐性进行统一的管理配置。 其开发技术还是使用spring,springmvc等等技术并没有多大改变。

Spring Boot设计的目的是让您尽可能快地启动和运行,并尽量减少前期的开销 。

Spring Boot 为了简化开发 大概分为了以下几个部分:

  • 统一的版本管理 使用spirng boot开发 前提必须指定父类依赖,具体如下:
  • maven版本

    12345
    parent    groupIdorg.springframework.boot/groupId    artifactIdspring-boot-starter-parent/artifactId    version2.0.2.RELEASE/version/parent

    parent
        groupIdorg.springframework.boot/groupId
        artifactIdspring-boot-starter-parent/artifactId
        version2.0.2.RELEASE/version
    /parent

    Gradle版本

    1234567891011121314
    buildscript {    repositories {        jcenter()        maven { url 'https://repo.spring.io/snapshot' }        maven { url 'https://repo.spring.io/milestone' }    }    dependencies {        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.0.BUILD-SNAPSHOT'    }} apply plugin: 'java'apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management'

    buildscript {
        repositories {
            jcenter()
            maven { url ‘https://repo.spring.io/snapshot' }
            maven { url ‘https://repo.spring.io/milestone' }
        }
        dependencies {
            classpath ‘org.springframework.boot:spring-boot-gradle-plugin:2.1.0.BUILD-SNAPSHOT’
        }
    }

    apply plugin: ‘java’
    apply plugin: ‘org.springframework.boot’
    apply plugin: ‘io.spring.dependency-management’

    • 集成的依赖管理

    Spring Boot为我们们提供了简化企业级开发绝大多数场景的 starter pom,只要使用了应用场景所需要的 starter pom,相相关的技术配置将会消除,就可以得到 Spring Boot为我们提供的自动配置的Bean。 简单的说,就是你想进行什么样的应用开发,导入指定的starter依赖即可。

    比如,如果我们想使用 spring web开发就可以导入依赖:

    1234
    dependency          groupIdorg.springframework.boot/groupId          artifactIdspring-boot-starter-web/artifactId/dependency

    dependency
              groupIdorg.springframework.boot/groupId
              artifactIdspring-boot-starter-web/artifactId
    /dependency

    这个依赖提过了基于web的开发的基本配置,并且内部默认集成了tomcat

    SpringBoot快速入门

    具体请查看这篇博客

  • **Spring Boot application starters** `来源:https://blog.csdn.net/u010919351/article/details/80238579`
  • NameDescription备注|------
    • 单独的spirng应用

    Spirng boot 的开发和启动入口

    123456
    @SpringBootApplicationpublic class SpringbootDemoApplication {  public static void main(String[] args) {  SpringApplication.run(SpringbootDemoApplication.class, args);  }}

    @SpringBootApplication
    public class SpringbootDemoApplication {
      public static void main(String[] args) {
      SpringApplication.run(SpringbootDemoApplication.class, args);
      }
    }

    @SpringBootApplication 说明这是一个标准的spring入口类,spring 所有业务将从这个main方法作为入口进行操作。您可以将其打成一个jar文件作为独立的应用 运行。

    • 与其他技术结合的自动配置

    进入@SpringBootApplication,我们会看到其内部集成了很多注解,其中就包括了自动配置:

    1234567891011121314151617181920212223
    @Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class})} @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})})public @interface SpringBootApplication {     ... ...}

    @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
    )}

    @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}
    )}
    )
    public @interface SpringBootApplication {
         … …
    }

  • 统一的配置管理 `如果你想要对自动的配置进行修改,则需要创建配置文件`,其详细的配置你可以在官网查到https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#common-application-properties 配置文件有两种,任选其一: `下面名称是默认的文件名称,如果你想用自定义的名称,需要进行配置指定才可以`
    • application.properties
      123
      spring.profiles.active=devspring.datasource.data-username=rootspring.datasource.data-password=root

    spring.profiles.active=dev
    spring.datasource.data-username=root
    spring.datasource.data-password=root

    • application.yml

    yml文件格式语法参考:http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt

    12345678
    spring:  profiles:    active: prod  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://127.0.0.1:3306/test    username: root    password: root

    spring:
      profiles:
        active: prod
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: root

    Spring Boot 入门案例:

    如果你看了上面的引导部分,你可能知道进行Spirng 开发一共分为三块:

    1. 指定spirng boot父类依赖
    2. 导入指定的 starter pom 依赖。(当然,您可以不用Spring boot的集成starter pom依赖)
    3. 创建入口

    Spring boot 入门案例(一)

    文件结构

    需要注意,controller包不许在入口类中,因为入口类默认扫描本包及其子包。

    123456789101112131415161718
    │  pom.xml└─src    ├─main    │  ├─java    │  │  └─com    │  │      └─lifeibai    │  │          └─springboot    │  │              │  SpringbootDemoApplication.java    │  │              │    │  │              └─controller    │  │                      HelloWorldController.java    │  │    │  ├─resources    │  └─webapp    │          index.html    │    └─test        └─java

    │  pom.xml
    └─src
        ├─main
        │  ├─java
        │  │  └─com
        │  │      └─lifeibai
        │  │          └─springboot
        │  │              │  SpringbootDemoApplication.java
        │  │              │
        │  │              └─controller
        │  │                      HelloWorldController.java
        │  │
        │  ├─resources
        │  └─webapp
        │          index.html
        │
        └─test
            └─java

    SpringbootDemoApplication.java

    123456789101112
    package com.lifeibai.springboot; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class SpringbootDemoApplication {    public static void main(String[] args) {         SpringApplication.run(SpringbootDemoApplication.class, args);    }}

    package com.lifeibai.springboot;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class SpringbootDemoApplication {
        public static void main(String[] args) {

            SpringApplication.run(SpringbootDemoApplication.class, args);
        }
    }

    HelloWorldController.java

    12345678910111213141516171819
    package com.lifeibai.springboot.controller; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping; /** * Author:Alan * * @Date: 2018-06-12  12:43:05 * 简单的web程序 */@Controllerpublic class HelloWorldController {     @GetMapping("/hello")    public String hello(){        return "index.html";    }}

    package com.lifeibai.springboot.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;

    /**
     * Author:Alan
     *
     * @Date: 2018-06-12  12:43:05
     * 简单的web程序
     */
    @Controller
    public class HelloWorldController {

        @GetMapping(“/hello”)
        public String hello(){
            return “index.html”;
        }
    }

    index.html
    12345678910
    !DOCTYPE htmlhtml lang="en"head    meta charset="UTF-8"    titleSpring boot 入门案例(一)/title/headbodyh1Hello,Spirng Boot!!!/h1/body/html

    !DOCTYPE html
    html lang=”en”
    head
        meta charset=”UTF-8”
        titleSpring boot 入门案例(一)/title
    /head
    body
    h1Hello,Spirng Boot!!!/h1
    /body
    /html

    访问:

    SpringBoot快速入门

    spring boot默认的tomcat服务器,其默认8080端口,如果你想更改端口,需要创建application.properties中配置:server.port=80

    Spring boot 入门案例(二)–通过Spring Initializr快速创建spirng boot应用。

    官网地址:

    点击Generater Project会下载文件:

    SpringBoot快速入门

    解压文件,其文件结构:

    123456789101112131415161718192021222324252627282930
    C:.│  .gitignore│  mvnw│  mvnw.cmd│  pom.xml│├─.mvn│  └─wrapper│          maven-wrapper.jar│          maven-wrapper.properties│└─src    ├─main    │  ├─java    │  │  └─com    │  │      └─example    │  │          └─demo    │  │                  DemoApplication.java    │  │    │  └─resources    │      │  application.properties    │      │    │      ├─static    │      └─templates    └─test        └─java            └─com                └─example                    └─demo                            DemoApplicationTests.java

    C:.
    │  .gitignore
    │  mvnw
    │  mvnw.cmd
    │  pom.xml

    ├─.mvn
    │  └─wrapper
    │          maven-wrapper.jar
    │          maven-wrapper.properties

    └─src
        ├─main
        │  ├─java
        │  │  └─com
        │  │      └─example
        │  │          └─demo
        │  │                  DemoApplication.java
        │  │
        │  └─resources
        │      │  application.properties
        │      │
        │      ├─static
        │      └─templates
        └─test
            └─java
                └─com
                    └─example
                        └─demo
                                DemoApplicationTests.java

    Spring boot 入门案例(三)–通过IDEA创建Spirng boot应用

    1,新建project/module

    SpringBoot快速入门

    2,指定maven坐标以及spirngboot

    SpringBoot快速入门

    3,选择Starter

    SpringBoot快速入门

    4,创建项目

    SpringBoot快速入门

    文章如有错误,请您一定指出,感谢之至!
    如果你有不同的见解,欢迎留言
    图片可能来源于网络,如有侵权请告知。
    文章中的资料有时忘记书写来源,如果需求请告知
    最后:关注一下呗

    SpringBoot快速入门
    SpringBoot快速入门

    长按二维码识别关注

    本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

    本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

    原文链接:blog.ouyangsihai.cn >> SpringBoot快速入门


      转载请注明: 好好学java SpringBoot快速入门

     上一篇
    Springboot webSocket(一) Springboot webSocket(一)
    在构建Spring boot项目时已经提供webSocket依赖的勾选。webSocket是TCP之上的一个非常薄的轻量级层 ,webSocket主要的应用场景离不开即时通讯与消息推送,但只要应用程序需要在浏览器和服务器之间来回发送消息,就
    下一篇 
    SpringBoot自动配置原理入门 SpringBoot自动配置原理入门
    author:阿风/Alan 公众号:阿风的JAVA Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Sp