springboot 开发入门,及问题汇总
1 . springboot簡單介紹(http://projects.spring.io/spring-boot/)
????????現在的web項目幾乎都會用到spring框架,而要使用spring難免需要配置大量的xml配置文件,而springboot的出現解?? 決了這一問題,一個項目甚至不用部署到服務器上直接開跑,真像springboot所說:“just run”。
????????springboot的很多默認編碼方式都是utf-8,真是福利啊。
????org.spring 2013年新開發的框架springboot , 它讓一個單獨項目的創建變得更加的簡單,讓所有依賴spring的程序可以做到“just run”。springboot提供大量第三方libraries讓我們可以非常輕松的開始創建一個spring工程,甚至不需要再去配置一些繁瑣的xml配置文件
??? 框架特點:
????1:創建獨立的spring應用。
????2:嵌入Tomcat, Jetty Undertow 而且不需要部署他們。
????3:提供的“starters”poms來簡化Maven配置
????4:盡可能自動配置spring應用。
????5:提供生產指標,健壯檢查和外部化配置
????6:絕對沒有代碼生成和XML配置要求
2 . 簡單實例演示??
??? 本文全程使用Springboot當前版本1.2.2(當前,推薦)
??????
????一個簡單的helloworld ? ? 直接開始run? main方法就可以了? 控制臺我也不知道都干了什么,好像是開始部署了,
????但是沒有關聯到我的tomcat。
???
????瀏覽器就能直接訪問了。
???
3 . 步驟詳解
??? *注意事項:
????1.開發第一個springboot程序最好使用maven來搭建,文檔全程也是maven構建。
????2.springboot因為是一個最新開發的框架,所以只支持java6以上,java7最好,官方推薦java8。
????3.需要maven3.2以上版本支持。
????4.支持以下servlet容器??? ???????? ????
? ? ? ? ? ? ? ? ? ? ? ? 也可以將springboot程序部署到所有支持servlet3.0以上的容器
?????#開發第一個springboot應用? HelloWorld
??????? 我這里用的MyEclipse10?? java 6? maven 3.2.3?? tomcat? 7.0.55
????????新建web project? 并添加maven支持。
??????
??????next之后后面要選擇javaee 6.0的library?? 記住不要選上,因為里面的slf4j會跟springboot自帶的產生沖突。
????? #配置pom.xml
????????配置pom的時候碰到了很多問題。這里提醒大家一下:
<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>springboot</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>springboot</name><description?/><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--?這里一定要配置上java的版本,如果是1.7版本的可不用配置?--><java.version>1.6</java.version><!--?配置你的tomcat版本?--><tomcat.version>7.0.55</tomcat.version></properties><build><plugins><!--如果是通過parent方式繼承spring-boot-starter-parent則不用此插件<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>--></plugins></build><!--?父依賴?--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.2.RELEASE</version></parent><dependencies><dependency><!--?導入jar包?--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>1.????springboot 的logback-classes-1.1.2.jar的包下有一個org.slf4j.impl包 這是springboot真正需要的包而MyEclipse自帶的javaEE6.0library里也有一個slf4j包但它不是springboot所需要的,會一直報 NoSuchMethod異常getSingleton()。搞了半天才找到,所以一開始暫時不添加javaEE6.0Library。
2.這里教大家一個快速找到class文件真正所處包的方法。
當無法確定某個類屬于哪個包是?? 可以通過Test.class.getProtectionDomain();來查看
例如:發生noSuchMethod異常時,但是確實有該方法,一般就是重復加載了jar包。
3.官方文檔的例子都是用java7運行的。不配置<java.version>1.6</java.version>的話可能 會報版本異常的錯誤。具體是啥忘了? 類似mirro.minor什么51.0的?? 50表示jdk1.6?? 51是jdk1.7
4.如果也不配置tomcat版本的話springboot默認會使用8.x版本的tomcat。所以要加一個
<tomcat.version>7.0.55</tomcat.version>來指定你所使用的tomcat版本(視你CATALINA_HOME配 置的所定)。
???????
?#編寫java代碼
????????
package?com.i.springboot.controller;import?org.springframework.boot.SpringApplication; import?org.springframework.boot.autoconfigure.EnableAutoConfiguration; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RestController; public?class?FirstController?{(value="/")//是springmvc中的注解String?home(){return?"helloworld";}public?static?void?main(String[]?args)?throws?Exception?{SpringApplication.run(FirstController.class,?args);}} @EnableAutoConfiguration注解用來自動配置,我們pom中配置了 ???spring-boot-starter-web所以spring會來創建一 個web應用來配置程序
? 完成后運行main程序無報錯則運行成功。在瀏覽器輸入相應的路徑即可獲得@RequestMapping返回的數據。
4 .? 一個標準的springboot程序結構應該是什么樣?
?? 1. spring通常建議我們將main方法所在的類放到一個root包下,@EnableAutoConfiguration(開啟自動配置)注解通常都放到main所在類的上面,下面是一個典型的結構布局,供參考
???
com+-?example+-?myproject+-?Application.java//main方法所在類在最底層|+-?bean????????????//bean類|???+-?Customer.java|???+-?CustomerRepository.java|+-?service?????????//service層|???+-?CustomerService.java|+-?web?????????????//controller層+-?CustomerController.java??? 從整體看去跟我們平時的布局差不多,就是將main方法放到了最底層。
????這樣@EnableAutoConfiguration可以從逐層的往下搜索各個加注解的類,例如,你正在編寫一個JPA程序(如果你的pom里進行了配置的話),spring會自動去搜索加了@Entity注解的類,并進行調用。
????通常我們只需要一個@EnableAutoConfiguration類
????2. spring通常建議我們在進行配置的時候盡量使用@注解的方式,盡管現在網上有各種各樣成熟的xml配置方式,如果你實在不想用注解(我目前還不會怎么用注解配置。。。)可以通過@ImportResource方式導入xml文件。
????下面是一個加載xml文件配置的例子(官方實例)。
? 3. 自動配置對程序沒有影響,我們隨時可以修改自己的配置來替代自動配置。例如,如果我們添加自己的數據源,那么spring默認的將不再使用。如果你一定要消除某些特定配置可以這樣來,如下所示:
import?org.springframework.boot.autoconfigure.*; import?org.springframework.boot.autoconfigure.jdbc.*; import?org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public?class?MyConfiguration?{}????4.? 使用@SpringbootApplication注解? 可以解決根類或者配置類(我自己的說法,就是main所在類)頭上注解過多的問題,一個@SpringbootApplication相當于@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他們的默認屬性值。
package?com.example.myproject; import?org.springframework.boot.SpringApplication; import?org.springframework.boot.autoconfigure.SpringBootApplication; ?//等同于?@Configuration?@EnableAutoConfiguration?@ComponentScanpublic class?Application?{????public?static?void?main(String[]?args)?{SpringApplication.run(Application.class,?args);}}
5. springboot功能全解.
??? 1.SpringApplication? 程序入口
????????SpringApplication.run(MySpringConfiguration.class, args);
????2.自定義打印項(就是控制臺輸出內容)????
????????在classpath下加入banner.txt 來定義程序啟動時要輸出的內容,例如我這樣
????????
????????
????banner的變量:
Variable
Description
${application.version}
MANIFEST.MF中的版本號例如1.0
${application.formatted-version}
格式化后的版本號,就是加了個V,例如V1.0…
${spring-boot.version}
springboot版本號,例如1.2.2.RELEASE.
${spring-boot.formatted-version}
格式化后的Springboot版本號,例如V1.2.2.RELEASE………
????3. 自定義SpringApplication
????????可以自定義一些應用的配置,如下關閉banner輸出:
public?static?void?main(String[]?args)?{SpringApplication?app?=?new?SpringApplication(MySpringConfiguration.class);app.setShowBanner(false);app.run(args); }????????SpringApplication的一些方法:
????????
??????? SpringApplication的構造器參數往往是一個類.class,而這個類一定是加有@Configuration注解的,另外還可以換成xml的配置路徑哦,前面有寫出來過,SpringApplication.run("classpath:/META-INF/application-context.xml",args);
???? 4. 流暢的創建API
????5.? 程序的事件和監聽
除了通常的Spring框架的事件,如ContextRefreshedEvent SpringApplication發送一些額外的應用程序事件。觸發一些事件實際上是ApplicationContext之前創建。
????????除了一些常見的spring時間,像ContextRefreshedEvent? SpringApplication會產生一些額外的事件,某些事件甚至會再ApplicationContext創建之間觸發。你可以通過很多方式創建監聽器,一般最常用的就是如下:
public?static?void?main(String[]?args)?throws?Exception?{SpringApplication?app?=?new?SpringApplication(FirstController.class);app.addListeners(new?TestListener());app.run(args); }付一個自定義的listener。import?org.springframework.boot.context.event.ApplicationStartedEvent; import?org.springframework.context.ApplicationListener; public?class?TestListener?implements?ApplicationListener<ApplicationStartedEvent>{public?void?onApplicationEvent(ApplicationStartedEvent?event)?{/*do?something*/} }
????程序事件運行順序:
An?ApplicationStartedEvent?is sent at the start of a run, but before any processing except the registration of listeners and initializers.
An?ApplicationEnvironmentPreparedEvent?is sent when the Environment to be used in the context is known, but before the context is created.
An?ApplicationPreparedEvent?is sent just before the refresh is started, but after bean definitions have been loaded.
An?ApplicationFailedEvent?is sent if there is an exception on startup.
總結
以上是生活随笔為你收集整理的springboot 开发入门,及问题汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BigDecimal add方法问题:调
- 下一篇: 如何将比Long类型更大数值字符串转化为