Spring Boot特点
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件(此处需要注意新建maven项目后不可手动配置tomcat)
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置
从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。简便起见,该框架也提供了命令行界面,它可以用来运行和测试Boot应用。框架的发布版本,包括集成的CLI(命令行界面),可以在Spring仓库中手动下载和安装。一种更为简便的方式是使用Groovy环境管理器(Groovy enVironment Manager,GVM),它会处理Boot版本的安装和管理。Boot及其CLI可以通过GVM的命令行gvm install springboot进行安装。在OS X上安装Boot可以使用Homebrew包管理器。为了完成安装,首先要使用brew tap pivotal/tap切换到Pivotal仓库中,然后执行brew install springboot命令。
要进行打包和分发的工程会依赖于像Maven或Gradle这样的构建系统。为了简化依赖图,Boot的功能是模块化的,通过导入Boot所谓的“starter”模块,可以将许多的依赖添加到工程之中。为了更容易地管理依赖版本和使用默认配置,框架提供了一个parent POM,工程可以继承它。
==============================Spring Boot项目最最基础实践==================================
1、新建maven项目。(正常建立即可,此处我建立的项目为mavenboot)
2、编写pom.xml文件。引入<parent>属性用于继承Spring Boot
3、编写Application,此处笔者放在java/com/mm/mavenboot包下面。
4、编写Example.class 作为Controller
5、启动项目。此时如果maven项目有红叉,只需要右键项目→Maven→Update Project
此处需要注意不要在Performance中的Project Facts中设置Dynamic Web Modul中设置runtime Tomcat;但需要注意此处的jdk是否与自己当前环境匹配。
====================================报错信息处理 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]
此处报错是因为jdk版本不对应。我的Eclipse默认jdk为1.7,但是项目设置的为1.6.只需要修改jdk为匹配即可。
但是上述这种方法治标不治本,我们可以在pom.xml中强制指定。
====================================报错信息处理 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
这个问题的原因就出在,加这3个注解的类 @Configuration,@ServletComponentScan,@EnableAutoConfiguration 和 SpringApplication.run(GameServer.class, args); 中的.class类不是同一个类的问题,保持一致就好了。注解加在哪,run方法中传入的class就是谁。如同上面的Example类。