SpringBoot教程(二) | 创建SpringBoot项目

转载一缕82年的清风大佬的文章(仅仅供个人学习用):
SpringBoot教程(二) | 创建SpringBoot项目

接下来我们要学习一下如何创建一个spring boot项目。 我们采用的环境信息
JDK1.8
Maven 3.6
Idea 2021.1
所谓spring Boot项目,其实本质上就是一个maven/gradle项目,这是里面通过引入springBoot专门设置好的依赖,完成整个项目开发流程的简化。所以我们就先创建一个maven 项目即可。当然也可以采用 spring initiallizer进行创建(需联网)。

我们就用创建普通maven项目的方式来演示。
打开我们的idea, 创建一个新的项目: 项目名称: springboot-learning

创建maven项目:


1.1. 填写项目名称


1.2. 添加依赖

点击Finish以后,其实我们的maven项目就已经创建完毕了,但是它现在还不是一个spring boot的项目,我们需要把它变成一个spring boot项目。其实就是添加一下依赖即可。我们创建好的项目结构如下。


接下来是添加依赖,让其成为springBoot项目。
添加的方式有两种:
一个是通过继承springBoot项目的方式,一种是在 标签中添加依赖。

成为springBoot项目实现方式一(直接继承starter parent项目)

在pom文件中添加如下依赖

<parent> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.6.2</version> 
  <relativePath/> <!-- lookup parent from repository --> 
</parent>

然后点击项目右侧maven的刷新按钮,等到依赖下载完毕就可以了。前提是maven一定要配置正确。

完整pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lsqingfeng.springboot</groupId>
    <artifactId>springboot-learning</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

成为springBoot项目实现方式二(自己添加所需的starter依赖)

当然如果我们不想采用继承的方式,或者由于我们的项目需要继承公司内部的一些组件而导致无法再继承其他项目,我们也可以采用下面这种方式。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.6.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

就是在 中添加依赖。完整pom如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.lsqingfeng.springboot</groupId>
    <artifactId>springboot-learning</artifactId>
    <version>1.0.0</version>

   <-- 定义可复用的变量 -->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


   <-- dependencyManagement的作用
   可以统一管理项目的版本号,确保应用的各个项目的依赖和版本一致
       特别是该项目作为其他项目的父模块时 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

但是要注意上面我们只是引用了springBoot的依赖管理,但是还并没有进行使用,所有我们当前真正依赖的jar包还没有。接下来我们就可以按需添加。
比如我们现在要进行web的开发,那么我们就需要有引入web相对应的依赖。 在pom 文件中添加 依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

添加后的完整pom

<?xml version="1.0" encoding="UTF-8"?>
<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.lsqingfeng.springboot</groupId>
    <artifactId>springboot-learning</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

这里无需写版本号,因为我们已经引入了springBoot的版本号2.6.2. 在他的统一pom文件中已经对所有的经常使用的模块做了统一的依赖管理。感兴趣的可以点进去springBooot的pom中去看看。

1.3. 创建启动类

添加完依赖之后,我们的项目就已经是spring boot 项目了,接下来我们来启动这个项目。首先在src/main/java下创建一个包: com.lsqingfeng.springboot,然后在这个包下在创建一个类名字就叫

SpringBootLearningApplication. 注意千万别叫SpringBootApplication, 或者SpringApplication这类的名字,因为很容易跟springBoot中的类名重复,导致一些莫名其妙的错误。结构如下:

1.4. 开发启动类程序,启动项目。

在刚刚创建的启动类中写一个main方法,添加一个注解就可以了。具体代码如下:

package com.lsqingfeng.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @className: SpringBootLearningApplication
 * @description: springboot启动类
 * @author: sh.Liu
 * @date: 2022-01-10 14:36
 */
@SpringBootApplication
public class SpringBootLearningApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootLearningApplication.class, args);
    }
}

开发完之后,我们直接运行这个main方法,这个项目就已经启动了。端口号是8080,只不过我们还没有写任何的接口而已。

通过上面的日志,我们不难判断出,这是使用的web容器是tomcat, 启动的端口号是8080。

但是初学者可能会有疑惑,没什么是tomcat呢,我们也没有依赖tomcat啊,哪里出来的呢,这就是springBoot带来的好处,我们仅仅依赖了一个 spring-boot-starter-web 这个依赖,就可以启动web程序了,其实,玄妙之处,都在这个依赖里,springBoot已经帮我们把web常用的依赖都打入到了spring-boot-starter-web 中,这样多余的事情就不需要我们关心了。像tomcat其实也包含在了这个依赖当中。我们可以去看一看。

spring-boot-starter-web

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <!-- This module was also published with a richer model, Gradle metadata,  -->
  <!-- which should be used instead. Do not delete the following line which  -->
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
  <!-- that they should prefer consuming it instead. -->
  <!-- do_not_remove: published-with-gradle-metadata -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.6.2</version>
  <name>spring-boot-starter-web</name>
  <description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
  <url>https://spring.io/projects/spring-boot</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.6.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-json</artifactId>
      <version>2.6.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.6.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.3.14</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.14</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

这个就是spring-boot-starter-web 中包好的所有依赖了,里面有tomcat, webmvc, json。这就是springBoot带来的按需加载的好处,并且整个项目,我们只写了一个启动了,加了一个注解就完成了,springBoot完全简化了配置。 我们访问一下8080

报了404,虽然报错了,因为我们没有写任何的接口,但是出现这个页面起码证明了我们服务已经启动了。

好了本篇内容就到这。后面我们继续深入学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值