SpringBoot快速启动和建立统一父pom

一,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项目,我们要有一个父项目来管理它所有子模块的依赖,还有版本的控制,使得我们更加方便管理我们的项目。
下面是我建立的项目目录截图:
这里写图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot多模块项目中,POM主要是用来管理子模块的依赖关系和版本控制的。 通常情况下,POM中会包含以下依赖: 1. Spring Boot依赖:这些依赖包括Spring Boot核心依赖和各种Spring Boot Starter依赖,用于构建Spring Boot应用程序。 2. 公共依赖:这些依赖包括各种通用的依赖,例如数据库驱动程序、日志框架、JSON解析器等。 3. 插件依赖:这些依赖包括Maven插件和其他构建工具,用于编译、打包和部署Spring Boot应用程序。 例如,以下是一个典型的Spring Boot多模块项目的POM文件示例: ``` <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> <properties> <spring.boot.version>2.4.3</spring.boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.boot.version}</version> </plugin> </plugins> </build> </project> ``` 在这个例子中,POM定义了三个子模块(module1、module2、module3),并且包含了Spring Boot和其他公共依赖。它还定义了一个Maven插件,用于构建和打包Spring Boot应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值