springboot情操陶冶-初識springboot

前言:springboot因爲其輕便和去配置化等的特性已經被普遍應用,基於時代潮流以及不被鄙視,筆者因而開闢此篇開始認識springboothtml

前話

springboot是基於spring而開發的輕量級框架,因此在學習springboot以前,務必對spring的工做模式和源碼有必定的瞭解。筆者此處就不展開了,若是有興趣可直接戳如下連接閱讀便可
Spring源碼情操陶冶-ContextLoaderListenerjava

springboot框架概覽

what_is_springboot

具體更多的信息,可參考spring官網#springboot。筆者此處對上述的配圖做下簡單的翻譯web

springboot是什麼

  1. springboot讓用戶能夠更爲簡單的去建立獨立的、基於spring的應用程序而且運行簡單化。spring

  2. springboot執拗的認爲經過spring平臺和其餘的第三方包就能夠輕鬆的運行相應的程序。而且說明大部分的springboot應用只須要少部分的spring配置,也就是去配置化apache

springboot特色

  • 建立獨立的Spring應用程序springboot

  • 直接內置Tomcat/Jetty/UnderTow等web容器(去war方式部署運行)框架

  • 提供starter依賴以簡化用戶的構建配置maven

  • 當須要的時候自動配置Spring和第三方依賴包spring-boot

  • 提供準生產特徵,好比metrics(度量)、heath checks(健康檢查)、externalized configuration(外部化配置)學習

  • 無代碼生成和無XML配置要求

springboot用法

筆者此處不分析springboot的相關用法,相關內容在官方的文檔上已經提的很是清楚,有興趣的直接戳連接前往閱讀。Spring Boot Reference

普通方式運行

jdk(1.8)/springboot(2.0.3.RELEASE)/spring(5.0.7.RELEASE)


maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-springboot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

啓動類

package com.example.demo;

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

@SpringBootApplication
public class DemoSpringbootApplication {

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

web方式運行

jdk(1.8)/springboot(2.0.3.RELEASE)/spring(5.0.7.RELEASE)


maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo-springboot-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-springboot-web</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

啓動類

package com.example.demospringbootweb;

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

@SpringBootApplication
public class DemoSpringbootWebApplication {

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

小結

筆者以此篇做爲springboot的開篇,後續便對springboot的源碼做下簡單的分析,方便讀者和筆者知其因此然而瞭如其然

相關文章
相關標籤/搜索