Spring Boot特點(diǎn)
1. 創(chuàng)建獨(dú)立的Spring應(yīng)用程序
2. 嵌入的Tomcat塘淑,無需部署WAR文件(此處需要注意新建maven項(xiàng)目后不可手動(dòng)配置tomcat)
3. 簡(jiǎn)化Maven配置
4. 自動(dòng)配置Spring
5. 提供生產(chǎn)就緒型功能,如指標(biāo)玩荠,健康檢查和外部配置
6. 絕對(duì)沒有代碼生成和對(duì)XML沒有要求配置
從最根本上來講,Spring Boot就是一些庫(kù)的集合贼邓,它能夠被任意項(xiàng)目的構(gòu)建系統(tǒng)所使用姨蟋。簡(jiǎn)便起見,該框架也提供了命令行界面立帖,它可以用來運(yùn)行和測(cè)試Boot應(yīng)用∮蒲猓框架的發(fā)布版本晓勇,包括集成的CLI(命令行界面),可以在Spring倉(cāng)庫(kù)中手動(dòng)下載和安裝。一種更為簡(jiǎn)便的方式是使用Groovy環(huán)境管理器(Groovy enVironment Manager娘汞,GVM)显拜,它會(huì)處理Boot版本的安裝和管理疲恢。Boot及其CLI可以通過GVM的命令行g(shù)vm install springboot進(jìn)行安裝。在OS X上安裝Boot可以使用Homebrew包管理器铝噩。為了完成安裝,首先要使用brew tap pivotal/tap切換到Pivotal倉(cāng)庫(kù)中窿克,然后執(zhí)行brew install springboot命令骏庸。
要進(jìn)行打包和分發(fā)的工程會(huì)依賴于像Maven或Gradle這樣的構(gòu)建系統(tǒng)。為了簡(jiǎn)化依賴圖年叮,Boot的功能是模塊化的具被,通過導(dǎo)入Boot所謂的“starter”模塊,可以將許多的依賴添加到工程之中只损。為了更容易地管理依賴版本和使用默認(rèn)配置一姿,框架提供了一個(gè)parent POM,工程可以繼承它跃惫。
==============================Spring Boot項(xiàng)目最最基礎(chǔ)實(shí)踐==================================
1叮叹、新建maven項(xiàng)目。(正常建立即可爆存,此處我建立的項(xiàng)目為mavenboot)
2蛉顽、編寫pom.xml文件。引入<parent>屬性用于繼承Spring Boot
3终蒂、編寫Application蜂林,此處筆者放在java/com/mm/mavenboot包下面。
4拇泣、編寫Example.class ?作為Controller
5噪叙、啟動(dòng)項(xiàng)目。此時(shí)如果maven項(xiàng)目有紅叉霉翔,只需要右鍵項(xiàng)目→Maven→Update Project
此處需要注意不要在Performance中的Project Facts中設(shè)置Dynamic Web Modul中設(shè)置runtime Tomcat睁蕾;但需要注意此處的jdk是否與自己當(dāng)前環(huán)境匹配。
====================================報(bào)錯(cuò)信息處理 ?1========================================
2017-08-30 11:34:33.681 ERROR 4648 --- [? ? ? ? ? main] o.s.boot.SpringApplication? ? ? ? ? ? ? : Application startup failedjava.lang.UnsupportedClassVersionError: javax/annotation/ManagedBean : Unsupported major.minor version 51.0at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.6.0_29]at java.lang.ClassLoader.defineClassCond(Unknown Source) ~[na:1.6.0_29]at java.lang.ClassLoader.defineClass(Unknown Source) ~[na:1.6.0_29]at java.security.SecureClassLoader.defineClass(Unknown Source) ~[na:1.6.0_29]at java.net.URLClassLoader.defineClass(Unknown Source) ~[na:1.6.0_29]at java.net.URLClassLoader.access$000(Unknown Source) ~[na:1.6.0_29]at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.6.0_29]at java.security.AccessController.doPrivileged(Native Method) ~[na:1.6.0_29]at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.6.0_29]at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.6.0_29]at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.6.0_29]at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.6.0_29]at org.springframework.util.ClassUtils.forName(ClassUtils.java:250) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.registerDefaultFilters(ClassPathScanningCandidateComponentProvider.java:188) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.(ClassPathBeanDefinitionScanner.java:164) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.(ClassPathBeanDefinitionScanner.java:138) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.(ClassPathBeanDefinitionScanner.java:111) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.(ClassPathBeanDefinitionScanner.java:83) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:62) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.6.0_29]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.6.0_29]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.6.0_29]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.6.0_29]
at java.lang.Class.newInstance0(Unknown Source) ~[na:1.6.0_29]
at java.lang.Class.newInstance(Unknown Source) ~[na:1.6.0_29]
at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:77) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
at org.springframework.boot.SpringApplication.createApplicationContext(SpringApplication.java:529) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:299) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
at com.mm.mavenboot.Application.main(Application.java:19) [classes/:na]
此處報(bào)錯(cuò)是因?yàn)閖dk版本不對(duì)應(yīng)债朵。我的Eclipse默認(rèn)jdk為1.7子眶,但是項(xiàng)目設(shè)置的為1.6.只需要修改jdk為匹配即可。
但是上述這種方法治標(biāo)不治本序芦,我們可以在pom.xml中強(qiáng)制指定臭杰。
====================================報(bào)錯(cuò)信息處理 ?2========================================
2016-05-16 12:03:37.526 ERROR [restartedMain][SpringApplication.java:827] - Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at com.lingyu.GameServer.main(GameServer.java:17) [classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_73]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_73]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.3.5.RELEASE.jar:1.3.5.RELEASE]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
... 13 more
這個(gè)問題的原因就出在,加這3個(gè)注解的類 @Configuration,@ServletComponentScan,@EnableAutoConfiguration 和 SpringApplication.run(GameServer.class, args); 中的.class類不是同一個(gè)類的問題谚中,保持一致就好了渴杆。注解加在哪寥枝,run方法中傳入的class就是誰。如同上面的Example類磁奖。