鍒濆SpringBoot

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒濆SpringBoot相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%a1%b9%e7%9b%ae' title='椤圭洰'>椤圭洰   repos   conf   select   鍏朵粬   鏍规嵁   highlight   factor   nts   

1銆?span style="font-family: "Microsoft YaHei"">浠€涔堟槸springboot锛?/span>

1.姒傚康锛?/h2>

        Spring Boot鏄敱Pivotal鍥㈤槦鎻愪緵鐨勫叏鏂版鏋讹紝灞炰簬spring鏃椾笅鐨勪竴涓」鐩紝鍏惰璁$洰鐨勬槸鐢ㄦ潵绠€鍖栨柊Spring搴旂敤鐨勫垵濮嬫惌寤轰互鍙婂紑鍙戣繃绋嬨€傝妗嗘灦浣跨敤浜嗙壒瀹氱殑鏂瑰紡鏉ヨ繘琛岄厤缃紝瀹冧娇鐢ㄢ€滀範鎯紭浜庨厤缃€濈殑鐞嗗康锛屼粠鑰屼娇寮€鍙戜汉鍛樹笉鍐嶉渶瑕佸畾涔夋牱鏉垮寲鐨勯厤缃€備娇鐢╯pringboot寰堝鏄撳垱寤轰竴涓嫭绔嬭繍琛岀殑spring椤圭洰锛屽苟涓斿彲浠ュ嚑涔庝笉浣跨敤spring閰嶇疆鎴栬€呬娇鐢ㄥ緢灏戠殑閰嶇疆銆?/p>

2銆佺粨鏋勫拰浣跨敤锛?/h2>

銆€銆€銆€springboot鍜屾櫘閫氱殑javase椤圭洰鐩镐技锛屼竴涓櫘閫氱殑maven椤圭洰娣诲姞瀹冪殑渚濊禆灏辫銆俿pring鎻愪緵寰堝鐨剆tart pom绠€鍖杕aven鐨勪緷璧栧姞杞斤紝涓€鑸細閰嶇疆涓€涓猵arent渚濊禆锛屾帶鍒舵暣涓猻pringboot鐨勭増鏈紝濡傛灉鏄痺eb椤圭洰锛岄厤缃竴涓猻pring-boot-start-web渚濊禆锛屼細鑷姩鍏宠仈涓€浜涘父鐢ㄧ殑web渚濊禆锛屾瘮濡俿pringmvc銆乻ervlet鐢氳嚦瀹冧細宓屽叆涓€涓猼omcat瀹瑰櫒锛屽洜姝ゆ棤闇€鍐峵omcat鏈嶅姟鍣ㄤ腑杩涜鍙戝竷銆?nbsp; springboot浼氳嚜鍔ㄦ壂鎻弒rc/main/resources鏂囦欢澶逛笅鐨勯厤缃紝涓€鑸槸涓€涓猵roperties锛坅pplication.properties锛夋垨鑰厃aml鏂囦欢,閲岄潰閰嶇疆涓€浜涘皯閲忕殑蹇呰鐨勫簲鐢ㄩ厤缃紝渚嬪鏁版嵁搴撹繛鎺ヤ俊鎭€佹湇鍔″櫒鍚嶇О绔彛鍙蜂箣绫荤殑淇℃伅銆?/p>

銆€銆€缂栧啓springboot鐨勫叆鍙g被锛屼竴鑸懡鍚嶆槸xxxApplication.java锛屽叾浠栫殑绫婚兘鍦ㄥ畠鎵€鍦ㄧ殑鐩綍鎴栬€呭瓙鐩綍涓紝杩欐牱spring瀹瑰櫒浼氳嚜鍔ㄦ壂鎻忔湁鐩稿簲娉ㄨВ鐨勭被銆傚叆鍙g被涓婄殑娉ㄨВ鏄疭pringBootApplicaton,绫讳腑鏈変釜Main鏂规硶浣滀负绋嬪簭鍏ュ彛锛宻pringboot妗嗘灦鎻愪緵涓€涓柟娉曞惎鍔ㄩ」鐩瓵pplication.run(鏈被鍚?class锛宎rgs)銆備竴鐩磋繍琛屽氨鍚姩springboot椤圭洰銆?/p>

銆€銆€浠ヤ笂涓ゆ灏辨槸鏈€鍩烘湰鐨勯厤缃紝骞朵笖鏄竴涓憟鐜颁竴涓熀鏈殑springboot闆忓舰銆?/p>

3銆乻pringboot杩愯鍘熺悊锛?/h2>

銆€銆€鍏ュ彛娉ㄨВ@SpringBootApplication娉ㄨВ鏄竴涓粍鍚堟敞瑙o紝

銆€銆€瀹冨寘鍚獲configuration銆丂EnableAutoConfiguration鍜孈componentScan涓変釜娉ㄨВ缁勬垚銆?/p>

銆€銆€瀹冪殑鏍稿績鏄疈EnableAutoConfiguration娉ㄨВ銆傝繖涓敞瑙d腑鏈変竴涓狜Import娉ㄨВ锛屽畠瀵煎叆閰嶇疆鍔熻兘EnableAutoConfigurationImportSelector锛岃繖涓被鏈変釜鏂规硶鎵弿鍏锋湁/META-INF/spring.factories鏂囦欢鐨刯ar銆?/p>

銆€銆€spring.factories涓0鏄庝簡閭d簺鑷姩閰嶇疆銆傜劧鍚庢垜浠湪application.properties鏂囦欢涓繘琛岀浉搴旂殑閰嶇疆锛屾瘮濡傛暟鎹簱杩炴帴锛屼娇springboot涓殑鑷姩閰嶇疆涓殑鍙傛暟鏄垜浠渶瑕佺殑閰嶇疆銆?/p>

2銆?strong>涓轰粈涔堜娇鐢⊿pringboot锛燂紙Springboot鏈夊摢浜涗紭鐐癸紵锛?/strong>

銆€銆€銆€銆€锛?锛夎兘澶熷揩閫熷垱寤虹嫭绔嬭繍琛岀殑Spring椤圭洰涓庝富娴佹鏋堕泦鎴?nbsp;      锛堢嫭绔嬭繍琛岋級

銆€銆€銆€銆€锛?锛変娇鐢ㄥ祵鍏ュ紡鐨剆ervlet瀹瑰櫒锛屽簲鐢ㄦ棤闇€鎵撳寘鎴恮ar鍖?nbsp;           锛堜笂鎵嬪鏄擄級

銆€銆€銆€銆€锛?锛塻tarters鑷姩渚濊禆涓庣増鏈帶鍒?nbsp;                                            锛堣嚜鍔ㄩ厤缃級

銆€銆€銆€銆€锛?锛夊ぇ閲忕殑鑷姩閰嶇疆锛岀畝鍖栧紑鍙戯紝涔熷彲淇敼榛樿鍊?nbsp;                 锛堢畝鍖栭厤缃級

銆€銆€銆€銆€锛?锛夊噯鐢熶骇鐜鐨勮繍琛屽簲鐢ㄧ洃鎺?nbsp;                                             锛堝簲鐢ㄧ洃鎺э級

銆€銆€銆€銆€锛?锛変笌浜戣绠楃殑澶╃劧闆嗘垚                                                         锛堟棤浠g爜鐢熸垚鍜孹ML閰嶇疆锛?/p>

銆€銆€鐙珛杩愯  , 鏃犻渶閮ㄧ讲war鏂囦欢  , 鑷姩閰嶇疆 , 绠€鍖栭厤缃?, 搴旂敤鐩戞帶,  鏃犱唬鐮佺敓鎴愬拰XML閰嶇疆 

3銆?strong>Springboot鐨勬牳蹇冮厤缃枃浠舵湁鍝嚑涓紵浠栦滑鐨勫尯鍒槸浠€涔堬紵

銆€銆€銆€Spring Boot 鐨勬牳蹇冮厤缃枃浠舵槸 application 鍜?bootstrap 閰嶇疆鏂囦欢銆?/span>

銆€銆€銆€銆€銆€銆€1.application 閰嶇疆鏂囦欢杩欎釜瀹规槗鐞嗚В锛屼富瑕佺敤浜?Spring Boot 椤圭洰鐨勮嚜鍔ㄥ寲閰嶇疆銆?/p>

銆€銆€銆€銆€銆€銆€2.bootstrap 閰嶇疆鏂囦欢鏈変互涓嬪嚑涓簲鐢ㄥ満鏅€?/p>

      1. 浣跨敤SpringCloudConfig閰嶇疆涓績鏃讹紝杩欐椂闇€瑕佸湪 bootstrap 閰嶇疆鏂囦欢涓坊鍔犺繛鎺ュ埌閰嶇疆涓績鐨勯厤缃睘鎬ф潵鍔犺浇澶栭儴閰嶇疆涓績鐨勯厤缃俊鎭紱
      2. 涓€浜涘浐瀹氱殑涓嶈兘琚鐩栫殑灞炴€э紱
      3. 涓€浜涘姞瀵?瑙e瘑鐨勫満鏅紱

4銆?strong>Springboot 鐨勯厤缃枃浠舵湁鍝嚑绉嶆牸寮忥紵浠栦滑鏈変粈涔堝尯鍒紵

.properties 鍜?.yml锛屽畠浠殑鍖哄埆涓昏鏄功鍐欐牸寮忎笉鍚屻€?/span>

锛?锛?properties

 鎶€鏈浘鐗? src=

锛?锛?yml
鎶€鏈浘鐗? src=
 
鍙﹀锛?yml 鏍煎紡涓嶆敮鎸?@PropertySource 娉ㄨВ瀵煎叆閰嶇疆銆?/span>

5銆?strong>Springboot鐨勬牳蹇冩敞瑙f槸鍝釜锛熷畠涓昏鐢卞摢鍑犱釜娉ㄨВ缁勬垚

鍚姩绫讳笂闈㈢殑娉ㄨВ鏄疈SpringBootApplication锛屽畠涔熸槸 Spring Boot 鐨勬牳蹇冩敞瑙o紝涓昏缁勫悎鍖呭惈浜嗕互涓?3 涓敞瑙o細

  1銆丂SpringBootConfiguration锛氱粍鍚堜簡 @Configuration 娉ㄨВ锛屽疄鐜伴厤缃枃浠剁殑鍔熻兘銆?/span>
  2銆丂EnableAutoConfiguration锛氭墦寮€鑷姩閰嶇疆鐨勫姛鑳斤紝涔熷彲浠ュ叧闂煇涓嚜鍔ㄩ厤缃殑閫夐」锛?/span>
濡傚叧闂暟鎹簮鑷姩閰嶇疆鍔熻兘锛欯SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })銆?/span>
  3銆丂ComponentScan锛歋pring缁勪欢鎵弿銆?/span>

6銆佸紑鍚?Spring Boot 鐗规€ф湁鍝嚑绉嶆柟寮忥紵

銆€銆€1锛夌户鎵縮pring-boot-starter-parent椤圭洰
銆€銆€2锛夊鍏pring-boot-dependencies椤圭洰渚濊禆

7銆丼pring Boot 闇€瑕佺嫭绔嬬殑瀹瑰櫒杩愯鍚楋紵

銆€銆€鍙互涓嶉渶瑕侊紝鍐呯疆浜?Tomcat/ Jetty 绛夊鍣ㄣ€?/span>

8銆佹垜浠兘鍚﹀湪 spring-boot-starter-web 涓敤 jetty 浠f浛 tomcat锛?/h1>

銆€銆€銆€銆€銆€鍦?spring-boot-starter-web 绉婚櫎鐜版湁鐨勪緷璧栭」锛屽苟鎶婁笅闈㈣繖浜涙坊鍔犺繘鍘汇€?/span>

 1 <dependency>
 2     <groupId>org.springframework.boot</groupId>
 3     <artifactId>spring-boot-starter-web</artifactId>
 4     <exclusions>
 5         <exclusion>
 6             <groupId>org.springframework.boot</groupId>
 7             <artifactId>spring-boot-starter-tomcat</artifactId>
 8         </exclusion>
 9     </exclusions>
10 </dependency>
11 <dependency>
12     <groupId>org.springframework.boot</groupId>
13     <artifactId>spring-boot-starter-jetty</artifactId>
14 </dependency>

9銆佽繍琛?Spring Boot 鏈夊摢鍑犵鏂瑰紡锛?/h1>
1锛夋墦鍖呯敤鍛戒护鎴栬€呮斁鍒癲ocker瀹瑰櫒涓繍琛?/span>
2锛夌敤 Maven/ Gradle 鎻掍欢杩愯
3锛夌洿鎺ユ墽琛?main 鏂规硶杩愯

10銆丼pring Boot 鑷姩閰嶇疆鍘熺悊鏄粈涔堬紵

娉ㄨВ@EnableAutoConfiguration锛孈Configuration锛孈ConditionalOnClass灏辨槸鑷姩閰嶇疆鐨勬牳蹇冿紝棣栧厛瀹冩槸涓€涓厤缃枃浠讹紝鍏舵鏍规嵁绫昏矾寰勪笅鏄惁鏈夎繖涓被鑷姩鍘婚厤缃€?/span>

11銆丼pring Boot 鐨勭洰褰曠粨鏋勬槸鎬庢牱鐨勶紵

 1 cn
 2  +- javastack
 3      +- MyApplication.java
 4      |
 5      +- customer
 6      |   +- Customer.java
 7      |   +- CustomerController.java
 8      |   +- CustomerService.java
 9      |   +- CustomerRepository.java
10      |
11      +- order
12          +- Order.java
13          +- OrderController.java
14          +- OrderService.java
15          +- OrderRepository.java
杩欎釜鐩綍缁撴瀯鏄富娴佸強鎺ㄨ崘鐨勫仛娉曪紝鑰屽湪涓诲叆鍙g被涓婂姞涓?@SpringBootApplication 娉ㄨВ鏉ュ紑鍚?Spring Boot 鐨勫悇椤硅兘鍔涳紝濡傝嚜鍔ㄩ厤缃€佺粍浠舵壂鎻忕瓑銆傘€?

銆€銆€


以上是关于鍒濆SpringBoot的主要内容,如果未能解决你的问题,请参考以下文章

JDBC鍒濆锛孌AO妯″紡

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

UE4 鍒濆鏉愯川Fresnel鑺傜偣

SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段

Spring boot:thymeleaf 没有正确渲染片段

鍒濆叆闂ㄦ椂瑙佸埌鐨勪竴浜涙瘮杈冩湁鐢ㄧ殑鍑芥暟