spring boot 初試,springboot入門,springboot helloworld例子

由於項目中使用了spring boot ,以前沒接觸過,因此寫個helloworld玩玩看,當作springboot的一個入門例子。搜索 spring boot。獲得官方地址:http://projects.spring.io/spring-boot/html

本文脈絡:java

1.一句話介紹 spring boot是幹啥的。 git

2.爲啥要用spring boot.程序員

3.用一個helloworld 打開springboot。web

 

 

  • springboot是幹啥的,能夠吃嗎。

一句話:作過Javaweb開發的程序員,確定都知道spring框架。springboot就是一個可以快速搭起spring項目的神器。redis

  • 爲啥要用spring boot

  在沒有springboot以前,假如咱們須要使用spring來搭建一個項目,好比說搭建一個spring mvc的項目。咱們須要幹啥:spring

假如說咱們是使用Maven 那麼步驟是這樣的:apache

 1.新建一個maven 項目,package 方式爲war.瀏覽器

 2.加入相關依賴,什麼spring-bean,spring-context,spring-webmvc等等,在web.xml中配置spring listener,配置spring.xml之類的。tomcat

 3.在2完成以後,依賴都配置好了,編譯經過了,就能夠運行項目了。

假如咱們沒有使用maven,就是單純的建個web項目,而後處處copy jar包到webapp lib 下面,將會更麻煩,各類容易漏jar包。

因此,上面的方式,對於新手而言,可能一個小時也跑不起一個項目。而有了springboot以後可能只須要十幾分鍾。

 

  • 一個demo打開springboot。

 

1.新建maven項目,package方式爲war. (Maven的介紹,安裝與使用見 這裏

   使用maven的時候,maven會從倉庫中下載依賴的jar包,國外倉庫可能會很慢,因此最好有個私服。通常公司都會有私服,私服搭建看這裏

2.在pom文件spring boot須要的依賴,配置文件以下:

<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>com.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build />
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

 看這個pom文件,dependencies 節點下面只配置了一個 spring-boot-starter-web 依賴,加了一個parent配置,實在是太簡潔了。

 

3.編寫測試action,這個action跟你用其餘方式建立spring項目裏邊的action個沒啥區別,@controller 註解action類 ,@Requestmapping 映射請求地址。

  惟一不一樣的就是多了一個Main方法和EnableAutoConfiguration註解,這個main方法使用來運行這個demo的。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    String home() {
        return "Hello ,spring boot!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class, args);
        //運行以後在瀏覽器中訪問:http://localhost:8080/hello
    }
    
}

4.啓動項目,只須要運行上面代碼的main方法,運行成功,控制檯輸出以下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.4.RELEASE)

2016-05-10 15:21:46.299  INFO 6668 --- [           main] com.example.HelloController              : Starting HelloController on 0KE99GYM83Y5KGX with PID 6668 (D:\newspace\springbootdemo\target\classes started by Administrator in D:\newspace\springbootdemo)
2016-05-10 15:21:46.301  INFO 6668 --- [           main] com.example.HelloController              : No active profile set, falling back to default profiles: default
2016-05-10 15:21:46.337  INFO 6668 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy
2016-05-10 15:21:47.385  INFO 6668 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-05-10 15:21:47.399  INFO 6668 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-05-10 15:21:47.400  INFO 6668 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-05-10 15:21:47.482  INFO 6668 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-05-10 15:21:47.482  INFO 6668 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1148 ms
2016-05-10 15:21:47.714  INFO 6668 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-05-10 15:21:47.939  INFO 6668 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy
2016-05-10 15:21:47.991  INFO 6668 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.example.HelloController.home()
2016-05-10 15:21:47.993  INFO 6668 --- [           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-10 15:21:47.994  INFO 6668 --- [           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-10 15:21:48.014  INFO 6668 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-10 15:21:48.014  INFO 6668 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-10 15:21:48.048  INFO 6668 --- [           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-10 15:21:48.128  INFO 6668 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-05-10 15:21:48.187  INFO 6668 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-05-10 15:21:48.190  INFO 6668 --- [           main] com.example.HelloController              : Started HelloController in 2.166 seconds (JVM running for 2.401)
2016-05-10 15:22:03.171  INFO 6668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-05-10 15:22:03.171  INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-05-10 15:22:03.181  INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms

這是啥,這是應用啓動了啊。咱們以前在沒有使用springboot的時候,就是通常的spring項目啓動應用須要幹啥。以前是deploy到tomcat裏邊,而後啓動tomcat。

這裏我啥都沒幹,連web.xml都沒寫,直接運行這個Main方法就能夠了。

5.在瀏覽器中訪問,以下:

 

能夠看到,這種方式建立一個spring 的web項目比較方便。若是是傳統的spring項目,須要添加各類依賴,而後發佈到tomcat運行看效果。

這裏只須要不多的依賴引用配置,運行一個方法即可以直接在瀏覽器訪問。正如springboot官網上說的 Just run。

 

 

 

[本例代碼地址](http://git.oschina.net/dimixu/springbootdemo)

 

推薦閱讀:

[spring-data-rest的魔力 10分鐘實現增刪改查](http://www.javashuo.com/article/p-wmdozodn-dh.html)

[探索Java9 模塊系統和反應流](http://www.javashuo.com/article/p-ngubkybg-cn.html

 

[Lua腳本在redis分佈式鎖場景的運用](https://www.cnblogs.com/demingblog/p/9542124.html)

[redis單點、redis主從、redis集羣cluster配置搭建與使用](https://www.cnblogs.com/demingblog/p/10295236.html)

[Netty開發redis客戶端,Netty發送redis命令,netty解析redis消息](https://www.cnblogs.com/demingblog/p/9989699.html)

[spring如何啓動的?這裏結合spring源碼描述了啓動過程](https://www.cnblogs.com/demingblog/p/7443714.html)
[SpringMVC是怎麼工做的,SpringMVC的工做原理](https://www.cnblogs.com/demingblog/p/9925268.html)
[spring 異常處理。結合spring源碼分析400異常處理流程及解決方法](https://www.cnblogs.com/demingblog/p/9218271.html)

[Mybatis Mapper接口是如何找到實現類的-源碼分析](https://www.cnblogs.com/demingblog/p/9544774.html)
[使用Netty實現HTTP服務器](https://www.cnblogs.com/demingblog/p/9970772.html)
[Netty實現心跳機制](https://www.cnblogs.com/demingblog/p/9957143.html)
[Netty系列](https://www.cnblogs.com/demingblog/p/9912099.html)

相關文章
相關標籤/搜索