SpringBoot是Spring推出的众多产品中的一个。
SpringBoot可用来开发RestFul应用,也可用来开发传统的Web应用。当然SpringMVC也可以,SpringBoot最大的特点是不需要什么配置,而且可以通过一个main方法当普通java程序启动,因为它已经内嵌了一个tomcat。
官方提供了三种方式来起步:maven、Gradle和SpringBoot CLI。
我这里使用最常见的maven方式:
官方推荐的maven是3.2以上,jdk是1.6及以上。并且强烈推荐的是1.8.
首先是pom文件:
pom很简单继承spring-boot-starter-parent,依赖spring-boot-starter-web就可以了。还有
<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>org.lhc</groupId>
<artifactId>spbt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后是一个java类
@RestController
@EnableAutoConfiguration
public class Starter {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Starter.class, args);
}
}
通过main方法启动,然后浏览器访问http://localhost:8080/就可以看到Hello World!了。
启动是会在控制台看到springboot的输出:
今天就先写到这吧,下次继续深入。
转载请注明出处:http://blog.youkuaiyun.com/redstarofsleep
更多内容请关注微信公众号: