一,springboot快速启动
首先,介绍一下springboot,springboot的出现,解决了spring的很多不足之处,使得spring的霸主地位不可动摇,以前搭建的web项目最麻烦的就是测试,每次修改程序都要重新启动服务器,这使得浪费了开发人员大量的时间,但是使用springboot可以快速启动,加快开发时间,哈哈,这也是我的个人体验。下面我们开始写代码。
1.开发环境
开发springboot项目,首先要jdk1.8版本以上才能启动,然后要有一款的称手的开发工具,在这里我使用了springboot官网提供的Spring Tool Suite (STS)开发工具,(https://spring.io/guides/gs/spring-boot/)这是下载网址。其实这款工具和eclipse差不多,只不过是多了一些插件而已。
2.搭建项目
首先建立一个maven项目,注意,建立的是quickstart项目不是webapp项目,建立好maven项目后,这是我们要借助springboot提供的快速启动方式来构建我们的第一个springboot项目。到springboot官网,找到下面的这段代码,如下图:
如上图,我们第一步复制parent标签之间的这段代码,添加到我们的pom.xml文件中。然后将第二步的依赖加入到pom文件的依赖中。代码如下:
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<groupId>cn.shinelon.springboot</groupId>
<artifactId>bootfirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bootfirst</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
写完pom文件后,我们来进行最后一步就可以启动一个简单的springboot项目了。
在src/main/java目录下建立一个包,然后创建下面这个类。
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;
/**
* @author Shinelon
*
*/
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
首先我们先启动程序感受一下springboot的强大,如果使用IDEA/eclipse的话可以输入mvn spring-boot:run来启动程序,使用STS则可以直接单击右键run as启动程序,然后在浏览器直接输入localhost:/8080/就可以看到”hello world”的字样。哈哈,怎么样,是不是很快就启动了。下面简单介绍一下上面的那段代码。
1.SpringApplication是Spring Boot框架中描述spring应用的类,它的run()方法会创建一个应用上下文(Application Context),然后它判断类路径上的依赖,比如我们引入了spring-boot-starter-web,然后它会判断当前应用程序为一个web程序,然后启动内置Servlet容器(默认tomcat服务器)来处理HTTP请求。
2.SpringWebMvc会将接收到的HTTP请求分发给controller控制器,这里的@Controller,它会直接将返回值作为HTTP Response的body部分返回浏览器。
3.@RequestMapping("/")就是我们经常写的处理请求的URL路径,可以引导程序处理正确的请求路径。
至此,我们的一个SpringBoot程序搭建完毕,下面我们来建立一个统一的父pom来管理版本问题。
二,建立统一的父POM
首先,我们来重新建立一个maven项目,然后删除除了pom.xml文件的所有,删除干净后,我们来看我们的pom文件,这里,我们要引入一个依赖,到maven仓库搜索spring-boot-dependencies之后引入这个依赖。注意,我们第一个建立的是父项目,要将packaging改为pom形式。
<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>cn.shinelon.springboot</groupId>
<artifactId>microboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>microboot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>microboot-base</module>
</modules>
</project>
然后在这个父项目下建一个子模块,然后在子模块的pom文件中引入上面启动springboot项目的依赖。下面是子模块的pom文件
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.shinelon.springboot</groupId>
<artifactId>microboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.shinelon.springboot</groupId>
<artifactId>microboot-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>microboot-base</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
然后我们在子模块的src/main/java这个包下面建立一个类,如下。
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 SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
好了,至此我们搭建了一个统一的父pom以及其子模块,和启动springboot程序一样启动上面程序,我们会看到和上面 一样的结果,在这里为什么要统一父pom呢?对于来maven项目,我们要有一个父项目来管理它所有子模块的依赖,还有版本的控制,使得我们更加方便管理我们的项目。
下面是我建立的项目目录截图: