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

本文介绍了如何使用 Spring Boot 快速启动项目,并通过实例演示了从开发环境配置到项目搭建的具体步骤。此外,还讲解了如何通过统一的父 POM 来管理项目依赖和版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

### 如何创建一个 Spring Boot 后端项目 #### 初始化项目的步骤 对于创建新的 Maven 项目,可以选择使用 Spring Initializr 或者通过流行的 IDE 工具如 IntelliJ IDEA Eclipse 来完成这一过程[^1]。另外一种方式是在命令行环境下利用 `spring init` 命令来快速搭建基础架构。 当采用图形界面工具时,在本地计算机先部署好开发环境——即安装好 IntelliJ IDEA 是前提条件之一;之后借助于集成好的 Spring Initializr 插件能够便捷地生成所需工程框架并将其顺利导入到该IDE当中去操作[^2]。 #### 推荐的IDE配置 IntelliJ IDEA 被广泛认为是 Java 开发者的首选编辑器之一,它提供了强大的功能支持良好的用户体验。为了更好地管理维护 Spring Boot 应用程序,建议开发者们优先考虑此款软件作为主要的工作平台。 #### 必要的依赖设置 典型的 Spring Boot 项目会遵循特定目录布局模式: - **src/main/java/** 存放所有的业务逻辑实现代码; - **com.example.bootdemo** 表示默认包路径,可根据实际需求调整命名空间; - **BootdemoApplication.java** 文件包含了启动整个应用程序所需的入口函数 main() 方法; - **src/main/resources/** 下放置各类资源配置项,比如静态网页素材、视图模板以及核心属性设定文件 application.properties 或 application.yml ,后者用于指定服务器监听地址、数据源参数等一系列运行期选项; - 测试案例则统一置于 **src/test/java/** 内部以便分离生产版块与验证环节; - 整个项目由 **pom.xml** 控制着版本号、库列表及其编译规则等方面的内容[^3]。 ```xml <dependencies> <!-- Web support --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Development tools (optional) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- Other dependencies as needed... --> </dependencies> ``` 上述 XML 片段展示了 pom.xml 中可能包含的部分依赖关系声明样例,其中 spring-boot-starter-web 提供了构建 RESTful API 所必需的支持组件集合,而 spring-boot-devtools 则是一个可选模块,有助于提高迭代效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值