spring-boot 入门学习
生活随笔
收集整理的這篇文章主要介紹了
spring-boot 入门学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
閑話不扯,使用spring-boot ,建議掌握maven或是gradle。
sample01
1 新建一個pom
<?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>myproject</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.1.9.RELEASE</version></parent><!--?Additional?lines?to?be?added?here...?--></project>?2 now 我們需要搭建一個簡單web程序,你只需向pom中添加
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies>?3 ok 如果你使用IDE,請刷新一下工程,讓maven下載依賴,現在我們可以進行編碼了。
import?org.springframework.boot.*; import?org.springframework.boot.autoconfigure.*; import?org.springframework.stereotype.*; import?org.springframework.web.bind.annotation.*;@RestController @EnableAutoConfiguration public?class?Example?{???@RequestMapping("/")String?home()?{?????return?"Hello?World!";}????public?static?void?main(String[]?args)?throws?Exception?{SpringApplication.run(Example.class,?args);}}4 現在可以運行mvn spring-boot:run,來啟動項目了,springboot的依賴里面包括一個嵌入式的tomcat,所以我們的項目 ? ? ?實際上是跑在tomcat上的,瀏覽http://localhost:8080?,怎么樣是不是很方便。
5 看上面的
@EnableAutoConfiguration?這個是spring-boot的注解,它會幫助我們自動發現并完成簡單的spring配置。所以我們在這個項目中沒有配置的多余的 ? ?config類或是xml。
6 有的時候我們要進行打包發布等工作,那么你需要向pom中添加spring-boot的插件
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>7 執行mvn package 命令就行了。我們得到的不是喜聞樂見的war,而是一個jar包,jar中包含了依賴的構件,包括我上面提到的嵌入式tomcat。所以我們可以執行java -jar 來運行這個jar。
轉載于:https://my.oschina.net/u/572499/blog/348883
總結
以上是生活随笔為你收集整理的spring-boot 入门学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转:如何提高自己的归纳总结能力?
- 下一篇: 如果有一天,我老无所依,请把我丢在迪拜捡