• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

环境搭建使用IDEA创建SpringBoot项目详细

武飞扬头像
@来杯咖啡
帮助1

一、创建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
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaacck
系列文章
更多 icon
同类精品
更多 icon
继续加载