【环境搭建】使用IDEA创建SpringBoot项目详细步骤

一、创建springboot项目

(1)创建spring initializr工程

通过idea工具创建工程时,不再选择maven了而是选择spring initializr。然后去勾选相关依赖。

  • 如果默认网址不行可以换https://start.aliyun.com/

在这里插入图片描述注意:
1、项目坐标的名字不一定非要和包的名字一致(默认创建的时候,idea给我们设置是一致的,但是我们可以改):
在这里插入图片描述
在这里插入图片描述

(2)勾选需要的的依赖

在这里插入图片描述注意:
1、引入spring web依赖的目的:可以看一下右侧对spring web的介绍。如果不引人这个依赖,你的项目是启动不成功的。

(3)项目结构如下

点击Finish之后,生成的文件、目录如下。

1.pom

  1. 该项目的<parent>spring-boot-starter-parent
  2. 项目默认添加了两个依赖spring-boot-starter-webpring-boot-starter-test
<?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 https://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>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>spring-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-demo</name>
    <description>spring-demo</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.目录结构

  1. 自动给我们构建好test

在这里插入图片描述

3.测试类

  1. 测试类使用的注解是SpringBootTest

在这里插入图片描述

(4)如何在springboot项目中添加测试

https://blog.csdn.net/qq_43783527/article/details/124553412

二、SpringBoot读取application.properties文件中的值

(1)SpringBoot读取配置文件的方式

1、读取application.properties文件中的值的方式:
https://blog.csdn.net/weixin_42344117/article/details/118087641
2、读取非application.properties文件中的值的方式:
https://blog.csdn.net/weixin_42344117/article/details/117605507

(2)application.properties配置如下

email.message.pull.all.executor.corePoolSize = 10
email.message.pull.all.executor.maximumPoolSize = 50
email.message.pull.all.executor.keepAliveTime = 5
email.message.pull.all.executor.workQueueCapacity = 500

在这里插入图片描述

(3)SpringBoot集成lombok

lombok更多使用方式:
https://blog.csdn.net/itCatface/article/details/119038565

引入lombok依赖:

       <!--集成lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

在这里插入图片描述

使用@Setter、@Getter注解的包是lombok,不是jdk.nashorn.internal.objects.annotations下的注解;
在这里插入图片描述

(4)@ConfigurationProperties

package com.example.springdemo.config.executor;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "email.message.pull.all.executor")
public class MessagePullAllExecutorConfig {
    private Integer corePoolSize;
    private Integer maximumPoolSize;
    private long keepAliveTime;
    private Integer workQueueCapacity;
}

在这里插入图片描述

(5)测试

在这里插入图片描述

三、SpringBoot注入Bean

SpringBoot注入Bean的几种方式

https://blog.csdn.net/Smy_0114/article/details/122229852

我们采用:@Configuration+@Bean的方式。

@Configuration+@Bean的方式注入

在这里插入图片描述

  1. 注入了两个类型均为ThreadPoolExecutor的Bean;
  2. 方法的参数是MessagePullAllExecutorConfig(里面有值);

四、SpringBoot整合@Slf4j

SpringBoot集成@Slf4j:
http://t.csdn.cn/Hc1QO
在这里插入图片描述

  • 23
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@来杯咖啡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值