1 . springboot簡單介紹(http://projects.spring.io/spring-boot/)java
如今的web項目幾乎都會用到spring框架,而要使用spring不免須要配置大量的xml配置文件,而springboot的出現解 決了這一問題,一個項目甚至不用部署到服務器上直接開跑,真像springboot所說:「just run」。web
springboot的不少默認編碼方式都是utf-8,真是福利啊。
spring
org.spring 2013年新開發的框架springboot , 它讓一個單獨項目的建立變得更加的簡單,讓全部依賴spring的程序能夠作到「just run」。springboot提供大量第三方libraries讓咱們能夠很是輕鬆的開始建立一個spring工程,甚至不須要再去配置一些繁瑣的xml配置文件apache
框架特色:瀏覽器
1:建立獨立的spring應用。tomcat
2:嵌入Tomcat, Jetty Undertow 並且不須要部署他們。springboot
3:提供的「starters」poms來簡化Maven配置服務器
4:儘量自動配置spring應用。mvc
5:提供生產指標,健壯檢查和外部化配置app
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; @RestController @EnableAutoConfiguration public class FirstController { @RequestMapping(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文件配置的例子(官方實例)。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import sample.xml.service.HelloWorldService; public class SampleSpringXmlApplication implements CommandLineRunner { @Autowired private HelloWorldService helloWorldService; @Override public void run(String... args) { System.out.println(this.helloWorldService.getHelloMessage()); } public static void main(String[] args) throws Exception { //run的時候加載xml的配置 SpringApplication.run("classpath:/META-INF/application-context.xml", args); } } <!-- xml中與咱們平時見到的同樣。 --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:property-placeholder/> <bean id="helloService" class="sample.xml.service.HelloWorldService"/> <bean id="application" class="sample.xml.SampleSpringXmlApplication"/> </beans>
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; @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
經過SpringApplicationBuilder構建 new SpringApplicationBuilder() .showBanner(false) .sources(Parent.class) .child(Application.class) .run(args);
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>{ @Override 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.