SpringBoot之基礎

簡介

背景

J2EE笨重的開發 / 繁多的配置 / 低下的開發效率 / 復雜的部署流程 / 第三方技術集成難度大

特點

①?快速創建獨立運行的spring項目以及主流框架集成

②?使用嵌入式的Servlet容器,?應用無需達成war包

③?starters自動依賴和版本控制

④?大量自動配置,?簡化開發,?也可修改默認值

⑤?無需配置xml文件,?無代碼生成,?開箱即用

⑥?準生產環境的運行時應用監控

⑦?與云計算的天然集成

微服務

推薦martinfowler的一篇博客譯文: http://blog.cuicc.com/blog/2015/07/22/microservices/

微服務是一種架構風格,?一個應用應該是一組小型服務,?可以通過http的方式進行互通.

以前的架構:?單體應用(ALL IN ONE)

?

微服務(Microservices):?每一個服務都是可替代可升級的軟件單元

?

每個服務之間通過http進行通信:

?

準備工作

JDK1.8

maven3.3.9

IDEA_2018

配置指定maven的全局JDK版本:?在setting.xml文件中的<profiles></profiles>標簽中

<profile><id>jdk-1.8</id><activation><activeByDefault>true</activeByDefault><jdk>1.8</jdk></activation><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion></properties></profile>

第一個例子

①?創建一個maven工程(jar)

②?導入spring boot相關的依賴

<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.idea.springboot</groupId><artifactId>spring-boot-01-helloworld</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><!--將應用打包成一個可執行的jar包--><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

?③ 編寫一個主程序, 啟動Spring Boot應用

/*** @Date 18/12/06/006 - 8:54* @Desc springboot第一個例子* @SpringBootApplication  標注一個主程序類 說明這是一個Spring Boot應用*/
@SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //spring應用啟動起來 SpringApplication.run(HelloWorldMainApplication.class, args); } }

④?編寫相關的Controller

@Controller
public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello World!"; } }

⑤?運行主程序測試,?訪問:?http://localhost:8080/hello

⑥?簡化部署(使用插件打成jar,?插件在pom文件中已給出)

探究

①?pom文件

? 父項目:

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

再依賴父項目:

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

管理Spring?Boot的所有依賴版本:?導入依賴默認是不需要寫上版本的(沒有在dependencies管理中的除外)

?

?②?導入的依賴

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

spring-boot-starter:?Spring?Boot的場景啟動器

spring-boot-starter-web:?幫我們導入了web模塊正常運行所依賴的組件

?

Spring?Boot將所有的功能場景都抽取出來,?做成一個個的starter(啟動器),?只需要在項目中引入這些starter,?要用哪個功能就導入具體的starter即可.

③?主程序類/主入口類

@SpringBootApplication注解,?該注解所標準的類即為SpringBoot的主配置類

?

? ? @SpringBootConfiguration

? ? ? ? @Configuration:?配置類上的注解

? ? ? ? ? ? @Component:?組件注解

? ? ? ? ? ? ? ? 配置類:?同配置文件的作用, 也是容器的組件.

? ? @EbableAutoConfiguration:?開啟自動注解功能

? ? ? ? @AutoConfigurationPackage:?自動配置包(自動導包)

? ? ? ? ? ? @Import({Registrar.class}):?Spring的底層注解,?給容器中導入一個組件

? ? ? ? 將主配置類(@SpringBootApplication標注的類)的所在包及下面所有子包的所有組件都掃描到容器中

? ? ? ? @Import({ AutoConfigurationImportSelector.class}):?導入具體組件的選擇器

? ? ? ? 將所有需要導入的組件以全類名的方式返回,?這些組件就會被添加到容器中.

? ? ? ? 會給容器中導入非常多的自動配置類(xxxAutoConfiguration):?給容器中導入這個場景需要組件并配置好這些組件.

?

?

? ? ? ? 有了自動配置類,?免去了手動編寫配置注入功能組件的工作.

? ??SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, ClassLoader);

? ? ? ? 該方法從文件"META-INF/spring.factories"中獲取自動配置的類信息.

?

小結:?SpringBoot在啟動時從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,?將這些值作為自動配置類導入到容器中,?自動配置類就會生效,?進行自動配置工作,?所以并不是不需要進行以前的配置,?只是這些配置都由SpringBoot幫我們完成了. J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar

使用Spring?Initializr快速創建SpringBoot項目(需要聯網)

①?選擇Spring?Initializr

?

②?填寫信息

?

③?需要哪個模塊勾選哪個即可

?

④?next->finish

⑤?resources文件夾中的目錄結構

? ? static文件夾:?保存所有的靜態資源(js,?css...);

? ? templates文件夾:?保存所有的模板頁面(由于SpringBoot使用嵌入式的tomcat,?所以默認不支持jsp),?可以使用模板引擎? ? ? ? ? ? ? ? ? ? (freemarker/ thymeleaf)

? ? application.properties:?SpringBoot應用的配置文件(可以修改一些默認的設置)

轉載于:https://www.cnblogs.com/MilkyWayGalaxy/p/10088817.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/250067.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/250067.shtml
英文地址,請注明出處:http://en.pswp.cn/news/250067.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[Java核心技術(卷I)] - vscode手動編譯運行繼承類

參考 - P160~P161 主要有3個類: 一個測試類(ManagerTest)、一個子類(Manager)、一個父類(Employee) 注意點: -1. 使用 javac -d . *.java進行預編譯 目錄結構入下: 此時會生成目錄結構如下: 之后運行 java com.inheritance.ManagerTest 附上幾個類的代碼 // com.inhe…

mysql常用語句和函數

mysql語句如果長期不寫&#xff0c;就會忘掉&#xff0c;所以要時常復習&#xff0c;溫故而知新。 1.select length("中國人"),select char_length("中國人"); 2建立數據庫的語句 use new_schema;create table ta(id int primary key);這是小括號&#xff…

shiro框架@RequiresPermissions 解釋

RequiresAuthentication 驗證用戶是否登錄&#xff0c;等同于方法subject.isAuthenticated() 結果為true時。 RequiresUser 驗證用戶是否被記憶&#xff0c;user有兩種含義&#xff1a; 一種是成功登錄的&#xff08;subject.isAuthenticated() 結果為true&#xff09;&…

【Social Listening實戰】當數據分析遭遇心理動力學:用戶深層次的情感需求浮出水面...

本文轉自知乎 作者&#xff1a;蘇格蘭折耳喵 ————————————————————————————————————————————————————— 本文篇幅較長&#xff0c;分為五部分&#xff0c;在中間部分有關于心理分析工具的介紹&#xff0c;案例分散在第二部…

Python 字符串切片

#-*- coding:utf-8 -*-#字符串切片names "abcdefgh"切片語法 names[起始位置:終止位置:步長] 起始位置:即字符串的下標&#xff0c;可以是正序下標(0,1,2...)&#xff0c;也可以是逆序下標(-1,-2,-3...) 終止位置:也是字符串的下標&#xff0c;但是和起始位置下標不…

[Java核心技術(卷Ⅰ)] - 判斷相等

參考 - P184 public boolean equals(Object otherObject) {// a quick test to see if the objects are identicalif (this otherObject) return true;// must return false if the explicit parameter is nullif (otherObject null) return null;// if the classes dont ma…

Oracle 11g DG主庫節點2 ORA-00245: control file backup fail

--節點1報錯 Sun Dec 09 08:29:57 2018Control file backup creation failed: failure to open backup target file /u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_zwdb.ctl.Errors in file /u01/app/oracle/diag/rdbms/zwdb/zwdb2/trace/zwdb2_arc0_167660.trc:ORA-27037: …

hive字符函數

轉載于:https://www.cnblogs.com/ggzhangxiaochao/p/9222732.html

java動態編譯

編譯&#xff0c;一般來說就是將源代碼轉換成機器碼的過程&#xff0c;比如在C語言中中&#xff0c;將C語言源代碼編譯成a.out,&#xff0c;但是在Java中的理解可能有點不同&#xff0c;編譯指的是將java 源代碼轉換成class字節碼的過程&#xff0c;而不是真正的機器碼&#xf…

[c++] - 簡單的冒泡

#include <iostream> using namespace std;int main() {// 利用冒泡排序實現升序序列int arr[9] {4, 2, 8, 0, 5, 7, 1, 3, 9};cout << "排序前: " << endl;for (int i 0; i < 9; i){cout << arr[i] << " ";}cout <…

Python爬蟲之解析網頁

常用的類庫為lxml, BeautifulSoup, re(正則) 以獲取豆瓣電影正在熱映的電影名為例,urlhttps://movie.douban.com/cinema/nowplaying/beijing/ 網頁分析 部分網頁源碼 <ul class"lists"><liid"3878007"class"list-item"data-title"…

騰訊企業郵箱報錯 smtp.exmail.qq.comport 465, isSSL false

一、報錯 "smtp.exmail.qq.com" port 465, isSSL false 通過網上搜索查詢一些資料&#xff0c;推測是郵箱的配置出問題了。 二、修改郵箱配置 1 // 創建屬性2 Properties props new Properties();3 props.setProperty("mail.transport.protocol", "s…

spring與JDK版本對應關系

搭建spring框架得時候要考慮jdk的版本&#xff0c;提供一下參考 JDK 8 中可以使用 Spring Framework 5.x JDK 7 中可以使用 Spring Framework 4.x JDK 6 中可以使用 Spring Framework 4.x JDK 5 中可以使用 Spring Framework 3.x

Markdown預覽功能不可用解決方案

初學者在使用Markdown時也許會遇到這個問題 原因是電腦缺少一個組件&#xff0c;解決方案很簡單&#xff0c;安裝上就好了&#xff0c;以下是鏈接 http://markdownpad.com/download/awesomium_v1.6.6_sdk_win.exe轉載于:https://www.cnblogs.com/j9oker/p/10092829.html

Linux 中yum的配置

1.進入yum的路徑 cd /etc/yum.repos.d 2.將原始的repo文件移入一個新建的backup文件下做備份 mv CentOS* backup 3.在/etc/yum.repos.d下新建一個自己的文件(這里的文件必須以repo結尾); vi zhi.repo 其中&#xff0c;第一行必須是[文件名]的格式  是一個標記 name*** 這是一…

[生態建設] - js判斷小技巧

0、參考 說明: 從幾個易得的點出發,逐步向外擴展延申,保證代碼的可靠性 1、判斷是否為某個類型 // 判斷是否為 null const isNull o > {return o null; };// 判斷是否為 undefined const isUndefined o > {return o undefined; };// 判斷是否為 null or undefined…

Spring中Bean的概念

一、Bean的定義 <beans…/>元素是Spring配置文件的根元素&#xff0c;<beans…/>元素可以包含多個<bean…/>子元素&#xff0c;每個<bean…/>元素可以定義一個Bean實例&#xff0c;每一個Bean對應Spring容器里的一個Java實例定義Bean時通常需要指定兩…

[TJOI2010]閱讀理解

題目描述 英語老師留了N篇閱讀理解作業&#xff0c;但是每篇英文短文都有很多生詞需要查字典&#xff0c;為了節約時間&#xff0c;現在要做個統計&#xff0c;算一算某些生詞都在哪幾篇短文中出現過。 輸入輸出格式 輸入格式&#xff1a; 第一行為整數N&#xff0c;表示短文篇…

ccentos 7下安裝php5.6并使用nginx + php-fpm部署多個不同端口網站

作為一個的勤雜工&#xff0c;近期因公司內部信息化的需求&#xff0c;給新進員工提供基礎的知識培訓和介紹&#xff0c;也為了給公司內部建立一個溝通交流的平臺&#xff0c;百度找了開源的百科系統HDwiki和開源的問答系統Tipask問答系統&#xff0c;蛋痛的這兩套系統均是phpm…