1.  我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用gradle来构建的话基本也一样)然后新建Spring相关的xml文件,而且往往那些xml文件还不会少。然后继续使用tomcat或者jetty作为容器来运行这个工程。基本上每次创建一个新的项目都是这么一个流程,而我们有时候仅仅想快速的创建一个Spring web工程来测试一些东西,或者是希望能节省时间。</span>  

 现在我们使用Spring Boot就可以快速的做到这些了。

 我们先来看一个非常简单的使用Spring boot的例子吧:

(1) 新建一个maven项目 比如springboottest

(2)配置pom.xml  引用spring boot 中快速搭建web项目的依赖,它会自动依赖一些其他的包,包括内置的 tomcat等

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   
  6.   <groupId>com.ut.workflow.design.parent</groupId>  
  7.   <artifactId>springboottest</artifactId>  
  8.   <version>0.0.1-SNAPSHOT</version>  
  9.   <properties>  
  10.  <spring.boot.version>1.1.4.RELEASE</spring.boot.version>  
  11. </properties>  
  12. <repositories>  
  13.           <repository>  
  14.                <id>com.springsource.repository.bundles.release</id>  
  15.                <name>Spring Maven Repository Repository</name>  
  16.                <url>http://repo2.maven.org/maven2/</url>  
  17.           </repository>  
  18.           <repository>  
  19.                <id>jboss</id>  
  20.                <url>https://repository.jboss.org/nexus/content/groups/public/</url>  
  21.           </repository>  
  22.           <repository>  
  23.                 <id>sonatype</id>  
  24.                 <name>Sonatype Repository</name>  
  25.                 <url>http://repository.sonatype.org/content/groups/public/</url>  
  26.           </repository>  
  27.     </repositories>  
  28. <dependencies>  
  29.      <dependency>  
  30.      <groupId>org.springframework.boot</groupId>  
  31.      <artifactId>spring-boot-starter-web</artifactId>  
  32.      <version>${spring.boot.version}</version>  
  33.      </dependency>  
  34. </dependencies>  
  35. <build>  
  36.      <plugins>  
  37.          <plugin>  
  38.              <groupId>org.springframework.boot</groupId>  
  39.              <artifactId>spring-boot-maven-plugin</artifactId>  
  40.             <version>${spring.boot.version}</version>  
  41.              <executions>  
  42.                  <execution>  
  43.                      <goals>  
  44.                         <goal>repackage</goal>  
  45.                      </goals>  
  46.                  </execution>  
  47.              </executions>  
  48.          </plugin>  
  49.      </plugins>  
  50. </build>  
  51. </project>  
如果下载比较慢,请更换上面repositry中的仓库地址

(3)编写简单的类

  1. import org.springframework.boot.SpringApplication;  
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  3. import org.springframework.context.annotation.EnableAspectJAutoProxy;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. /** 
  8.  * Hello world! 
  9.  * 
  10.  */  
  11. @RestController  
  12. @EnableAutoConfiguration  
  13. public class App   
  14. {    
  15.     @RequestMapping(value="/")  
  16.     String home(){  
  17.         return "hello world";  
  18.     }  
  19.     public static void main( String[] args )  
  20.     {  
  21.        SpringApplication.run(App.class, args);  
  22.     }  
  23. }  

(4)运行main函数,访问http://localhost:8080/

      网页会输出hello world 一个最简单的spring boot 项目就完成了


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区