文章目錄
  1. 1. SpringBoot特性
  2. 2. 一、创建SpringBoot工程
  3. 3. 二、Hello SpringBoot
  4. 4. 三、参考文档

摘要:微服务被各种互联网企业捡起来,小团队, 独立部署无影响,不限制人员技术栈,而SpringBoot乃是微服务架构的绝配,有点在省略了繁琐的文件配置。使用spring boot很容易创建相对独立,适用于生产环境的spring应用。


本次学习记录,记录了自己使用IDEA IntelliJ创建一个SpringBoot工程的过程

SpringBoot特性

Spring Boot具有如下特性:
1、 为基于Spring的开发提供更快的入门体验
2、开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
3、提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
4、Spring Boot并不是不对Spring功能上的增强,而是提供了一种快速使用Spring的方式。

一、创建SpringBoot工程

  1. 打开IntelliJ IDEA
    Alt text
  2. 选择Spring Initialzr,并选择工程的SDK,生成服务的URL已经自动填写完毕
    Alt text
  3. 进入Maven工程配置步骤,包括工程名,工程类型,打包,Java Version等信息,依照个人需要进行填写
    Alt text
  4. 下一个步骤选择SpringBoot版本和其他依赖
    Alt text
  5. 最后一个步骤,完成工程设置
    Alt text
  6. 最后生成SpringBoot工程后,需要等一段时间下载依赖包
    Alt text

至此已经完成SpringBoot的创建,下面是完成测试

二、Hello SpringBoot

  1. 我们在该路径下会发现,自动生成了一个SpringBootTestApplication.java文件
    Alt text
  2. 我们可以试着运行这个静态方法,会发现运行成功后的控制台如下显示:
    Alt text

    1
     .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.3.5.RELEASE)
    
    2016-05-31 15:13:43.273  INFO 85150 --- [           main] com.guojohnny.SpringBootTestApplication  : Starting SpringBootTestApplication on huangguoxindeMacBook-Pro.local with PID 85150 (/Users/huangguoxin/IdeaProjects/SpringBoot-HelloWorld/target/classes started by huangguoxin in /Users/huangguoxin/IdeaProjects/SpringBoot-HelloWorld)
    2016-05-31 15:13:43.277  INFO 85150 --- [           main] com.guojohnny.SpringBootTestApplication  : No active profile set, falling back to default profiles: default
    2016-05-31 15:13:43.344  INFO 85150 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c7be4ce: startup date [Tue May 31 15:13:43 CST 2016]; root of context hierarchy
    2016-05-31 15:13:44.482  INFO 85150 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2016-05-31 15:13:44.492  INFO 85150 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2016-05-31 15:13:44.492  INFO 85150 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
    2016-05-31 15:13:44.574  INFO 85150 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2016-05-31 15:13:44.574  INFO 85150 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1232 ms
    2016-05-31 15:13:44.800  INFO 85150 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
    2016-05-31 15:13:44.803  INFO 85150 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
    2016-05-31 15:13:44.804  INFO 85150 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2016-05-31 15:13:44.804  INFO 85150 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2016-05-31 15:13:44.804  INFO 85150 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
    2016-05-31 15:13:44.950  INFO 85150 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7c7be4ce: startup date [Tue May 31 15:13:43 CST 2016]; root of context hierarchy
    2016-05-31 15:13:45.001  INFO 85150 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/SpringBoot/hello],methods=[GET],produces=[application/json]}" onto public java.lang.String com.guojohnny.WidgetController.helloSpringBoot()
    2016-05-31 15:13:45.003  INFO 85150 --- [           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-05-31 15:13:45.004  INFO 85150 --- [           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,javax.servlet.http.HttpServletResponse)
    2016-05-31 15:13:45.027  INFO 85150 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2016-05-31 15:13:45.027  INFO 85150 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2016-05-31 15:13:45.062  INFO 85150 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2016-05-31 15:13:45.165  INFO 85150 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2016-05-31 15:13:45.209  INFO 85150 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2016-05-31 15:13:45.213  INFO 85150 --- [           main] com.guojohnny.SpringBootTestApplication  : Started SpringBootTestApplication in 2.598 seconds (JVM running for 2.825)
  3. 创建一个控制器,实现Web应用Controller功能,宇SpringMVC实现大同小异,代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    /**
    * Created by huangguoxin on 16/5/31.
    */

    @RequestMapping("/SpringBoot")
    @RestController
    public class WidgetController {

    @RequestMapping(value = "/hello",method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE})
    public String helloSpringBoot(){
    return "Hello SpringBoot!";
    }
    }
  4. 运行SpringBootTestApplication,并在浏览器上访问该URL,获得结果:
    Alt text

  5. 至此完成初步使用IntelliJ IDEA创建SpringBoot工程

三、参考文档

http://www.jianshu.com/p/a35b25f4c1ec
https://segmentfault.com/a/1190000005020589
https://segmentfault.com/a/1190000000762888
http://my.oschina.net/u/1027043/blog/405784?fromerr=h678V9kq

文章目錄
  1. 1. SpringBoot特性
  2. 2. 一、创建SpringBoot工程
  3. 3. 二、Hello SpringBoot
  4. 4. 三、参考文档