1. 创建项目

直接使用IDEA提供的Spring Initializer即可,构建工具选择Gradle:

image-20250905213752976

依赖:

image-20250905213742360

构建完成后删除src目录,因为根目录属于管理模块目录不提供运行的应用:

image-20250905213718845

2. 修改build.gradle

这是最复杂的一步,并且Gradle版本更新的话步骤可能会不一样,首先在底部添加一个空的subprojects:

image-20250905214345452

接着把dependencies以及tasks.name(‘test’)移动进去:

image-20250905214413389

最后一步是,在subprojects开头,添加插件apply,根据默认初始化创建的plugins,逐一添加。

比如这里默认使用了三个插件:

image-20250905214450608

apply到subprojects中:

image-20250905214523875

3. 创建模块

File -> New -> Module:

image-20250905214629050

输入模块名即可,这里的例子是创建两个模块:

  • service
  • app

image-20250905214725506

image-20250905214754098

创建好后如图所示:

image-20250905214905334

完成创建之后,把两个模块中的build.gradle除了repositories之外的全部删去,仅保留repositories:

image-20250905214934842

4. 编写模块

4.1 service模块

首先创建包,根据根目录中的build.gradle中的group创建:

image-20250905215305014

image-20250905215320738

接着编写一个叫TestService的带@Service注解的类,里面包含一个test方法:

image-20250905215350046

同时修改service模块的build.gradle,添加bootJar以及jar选项:

1
2
3
4
5
6
7
bootJar{
enabled = false
}

jar{
enabled = true
}

image-20250905215433609

4.2 app模块

首先创建包:

image-20250905215457385

接着在app模块的build.gradle添加service模块的依赖:

image-20250905215612175

再创建启动类以及一个Controller:

image-20250905215623052

代码如下:

1
2
3
4
5
6
7
8
9
10
11
package com.example.app;

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

@SpringBootApplication(scanBasePackages = "com.example")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.controller;

import com.example.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TestController {
private final TestService service;
@GetMapping("/test")
public String test(){
return service.test();
}
}

注意,因为Spring Boot无法自动识别其他模块下的类,所以需要手动处理一下,有三种方法:

  • 第一种:使用@Import,也就是@Import(TestService.class)
  • 第二种:使用scanBasePackageClasses,也就是@SpringBootApplication(scanBasePackageClasses={TestService.class})
  • 第三种:使用scanBasePackages,也就是例子中的代码@SpringBootApplication(scanBasePackages = “com.example”)

5. 运行

接下来就可以运行了,可以直接点击Application旁边的绿色小三角:

image-20250905215831262

成功运行:

image-20250905220653531

同样可以访问localhost:8080/test:

image-20250905220641751

6. 测试

创建测试包、测试类:

image-20250905215949050

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.test;

import com.example.service.TestService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = TestService.class) //需要引入对应的类
public class JavaGradleTest {
@Autowired
private TestService service;

@Test
public void test() {
System.out.println(service.test());
}
}

接着进行测试:

image-20250905220041613

当然测试也可以跑一下Gradle中的任务:

image-20250905220517206

7. 打包

打包的话直接运行bootJar即可:

image-20250905220231079

会在build/libs下生成JAR包:

image-20250905220255282

测试:

image-20250905220457355

image-20250905220429604

再次访问localhost:8080/test没有问题。

这样使用Java+Gradle构建一个多模块的Spring Boot项目就成功了。


本站由 卡卡龙 使用 Stellar 1.29.1主题创建

本站访问量 次. 本文阅读量 次.