鍒濆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 nts1銆?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>
- 浣跨敤SpringCloudConfig閰嶇疆涓績鏃讹紝杩欐椂闇€瑕佸湪 bootstrap 閰嶇疆鏂囦欢涓坊鍔犺繛鎺ュ埌閰嶇疆涓績鐨勯厤缃睘鎬ф潵鍔犺浇澶栭儴閰嶇疆涓績鐨勯厤缃俊鎭紱
- 涓€浜涘浐瀹氱殑涓嶈兘琚鐩栫殑灞炴€э紱
- 涓€浜涘姞瀵?瑙e瘑鐨勫満鏅紱
4銆?strong>Springboot 鐨勯厤缃枃浠舵湁鍝嚑绉嶆牸寮忥紵浠栦滑鏈変粈涔堝尯鍒紵
.properties 鍜?.yml锛屽畠浠殑鍖哄埆涓昏鏄功鍐欐牸寮忎笉鍚屻€?/span>
锛?锛?properties
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 鐨勫悇椤硅兘鍔涳紝濡傝嚜鍔ㄩ厤缃€佺粍浠舵壂鎻忕瓑銆傘€?
1锛夋墦鍖呯敤鍛戒护鎴栬€呮斁鍒癲ocker瀹瑰櫒涓繍琛?/span>
2锛夌敤 Maven/ Gradle 鎻掍欢杩愯
3锛夌洿鎺ユ墽琛?main 鏂规硶杩愯
娉ㄨВ@EnableAutoConfiguration锛孈Configuration锛孈ConditionalOnClass灏辨槸鑷姩閰嶇疆鐨勬牳蹇冿紝棣栧厛瀹冩槸涓€涓厤缃枃浠讹紝鍏舵鏍规嵁绫昏矾寰勪笅鏄惁鏈夎繖涓被鑷姩鍘婚厤缃€?/span>
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的主要内容,如果未能解决你的问题,请参考以下文章
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段
SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段