spring boot --- 初級體驗

Spring boot的介紹我就很少說了,網上能夠本身看一下。java

 

它的優勢就是:快!適合小白,沒有複雜的配置文件。web

缺點也很明顯:坑有些多, 文檔略少,報錯有時不知道該如何處理。spring

 

下面作個最簡單的入門:apache

首先使用maven來建立一個項目,jar便可。瀏覽器

假設工程名字爲spring-boottomcat

而後在pom.xml中添加以下配置:app

<!-- Inherit defaults from Spring Boot -->  
<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>0.5.0.BUILD-SNAPSHOT</version>  
</parent>  
  
<!-- Add typical dependencies for a web application -->  
<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  
</dependencies>  
  
<!-- Package as an executable JAR -->  
<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-maven-plugin</artifactId>  
        </plugin>  
    </plugins>  
</build>  
  
<!-- Allow access to Spring milestones and snapshots -->  
<!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->  
<repositories>  
    <repository>  
        <id>spring-snapshots</id>  
        <url>http://repo.spring.io/snapshot</url>  
        <snapshots><enabled>true</enabled></snapshots>  
    </repository>  
    <repository>  
        <id>spring-milestones</id>  
        <url>http://repo.spring.io/milestone</url>  
        <snapshots><enabled>true</enabled></snapshots>  
    </repository>  
</repositories>  
<pluginRepositories>  
    <pluginRepository>  
        <id>spring-snapshots</id>  
        <url>http://repo.spring.io/snapshot</url>  
    </pluginRepository>  
    <pluginRepository>  
        <id>spring-milestones</id>  
        <url>http://repo.spring.io/milestone</url>  
    </pluginRepository>  
</pluginRepositories>  

以後咱們建立一個controller:maven

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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
@EnableAutoConfiguration
public class SimpleController {
 
    @RequestMapping(value ="/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello(){
        return "hello world";
    }
 
    public static void main(String[] args) {
        SpringApplication.run(SimpleController.class, args);
    }
}

這個controller裏有個main方法,看到沒,咱們直接運行便可,他會自動部署到tomcat或者jetty(8080端口不要被佔用)。spring-boot

而後在控制檯你會看到:學習

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.0.2.RELEASE)
 
2014-04-26 22:54:40.985  INFO 7236 --- [           main] c.r.spring.boot.SimpleController         : Starting SimpleController on rollen with PID 7236 (D:\workspace\GitHub\SpringDemo\spring-boot\target\classes started by wenchao.ren in D:\workspace\GitHub\SpringDemo\spring-boot)
2014-04-26 22:54:41.008  INFO 7236 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Sat Apr 26 22:54:41 CST 2014]; root of context hierarchy
2014-04-26 22:54:41.583  INFO 7236 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-04-26 22:54:41.706  INFO 7236 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-04-26 22:54:41.706  INFO 7236 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-04-26 22:54:41.785  INFO 7236 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-04-26 22:54:41.785  INFO 7236 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 779 ms
2014-04-26 22:54:42.055  INFO 7236 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-04-26 22:54:42.057  INFO 7236 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-04-26 22:54:42.289  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.368  INFO 7236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.rollenholt.spring.boot.SimpleController.hello()
2014-04-26 22:54:42.376  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.377  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.447  INFO 7236 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-04-26 22:54:42.459  INFO 7236 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-04-26 22:54:42.460  INFO 7236 --- [           main] c.r.spring.boot.SimpleController         : Started SimpleController in 1.675 seconds (JVM running for 1.944)
2014-04-26 22:54:54.963  INFO 7236 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-04-26 22:54:54.963  INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2014-04-26 22:54:54.971  INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 8 ms

固然還有一種啓動運行方式,就是經過@Configuration+@ComponentScan開啓註解掃描並自動註冊相應的註解Bean

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
  
@Configuration  
@ComponentScan  
@EnableAutoConfiguration  
public class Application {  
    public static void main(String[] args) {  
        SpringApplication.run(Application.class);  
    }  
}  

啓動完以後瀏覽器訪問一下:http://localhost:8080/hello

就是這麼快。

對於想學習/體驗Spring的新手,快速創建項目模型等能夠考慮用這種方式。

相關文章
相關標籤/搜索