听说最近spring boot很火,于是我也开始研究了起来,基本的原理我就不说了,说的直白点,就是把我们spring需要配置的东西都给封装了起来,不需要我们再去配置了,方便了我们更专注于业务的开发,而不用在担心各种配置了,直接上代码吧!
- 先创建一个简单的web maven项目,步骤就不说了。
在pom.xml中创建spring boot支持。
添加继承的父类项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
好了 pom.xml 就到此为止。
- 创建application.java
@RestController
@EnableWebMvc
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@RequestMapping("/")
String home(){
return "hello world !";
}
此时我们右键运行该main方法,spring boot就启动啦!启动日志如下
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.0.RELEASE)
2016-07-05 15:51:49.189 INFO 5980 --- [ main] com.boot.application.Application : Starting Application on zjgt0319-PC with PID 5980 (D:\Toby\myeclipse2014space\springbootexample\target\classes started by zjgt0319 in D:\Toby\myeclipse2014space\springbootexample)
2016-07-05 15:51:49.191 INFO 5980 --- [ main] com.boot.application.Application : No profiles are active
2016-07-05 15:51:49.239 INFO 5980 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6977f8ad: startup date [Tue Jul 05 15:51:49 CST 2016]; root of context hierarchy
2016-07-05 15:51:50.262 INFO 5980 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-07-05 15:51:50.270 INFO 5980 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-07-05 15:51:50.271 INFO 5980 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.28
2016-07-05 15:51:50.339 INFO 5980 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-07-05 15:51:50.339 INFO 5980 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1102 ms
2016-07-05 15:51:50.443 INFO 5980 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-07-05 15:51:50.447 INFO 5980 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-07-05 15:51:50.579 INFO 5980 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6977f8ad: startup date [Tue Jul 05 15:51:49 CST 2016]; root of context hierarchy
2016-07-05 15:51:50.682 INFO 5980 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.boot.application.Application.home()
2016-07-05 15:51:50.689 INFO 5980 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-07-05 15:51:50.689 INFO 5980 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2016-07-05 15:51:50.891 INFO 5980 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-07-05 15:51:50.954 INFO 5980 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-07-05 15:51:50.958 INFO 5980 --- [ main] com.boot.application.Application : Started Application in 2.043 seconds (JVM running for 2.275)
看到这个信息说明启动成功了(说明下spring boot是集成tomcat的),接下来我们访问http://localhost:8080/ 我们经典的 hello world !就出来了!好了,到此我们的spring boot就创建好了。
这是本人第一次写博客,希望各位大神多多指教,有什么错误的地方烦请指正!小弟在此谢过。下一篇文章我们将进一步学习spring boot。