我们以前使用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等
<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.ut.workflow.design.parent</groupId>
<artifactId>springboottest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.boot.version>1.1.4.RELEASE</spring.boot.version>
</properties>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>Spring Maven Repository Repository</name>
<url>http://repo2.maven.org/maven2/</url>
</repository>
<repository>
<id>jboss</id>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sonatype</id>
<name>Sonatype Repository</name>
<url>http://repository.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
如果下载比较慢,请更换上面repositry中的仓库地址
(3)编写简单的类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@RestController
@EnableAutoConfiguration
public class App
{
@RequestMapping(value="/")
String home(){
return "hello world";
}
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
(4)运行main函数,访问http://localhost:8080/
网页会输出hello world 一个最简单的spring boot 项目就完成了