一般来说创建一个springboot工程基本就可以了,但是有的时候可能需要将业务模块逻辑划分,每块业务模块都是一个工程,下边演示下多模块进行开发

 

目录结构

...somefun

......somefun-web

......somefun-service-system

.........somefun-system-api

.........somefun-system-service

 

somefun项目父工程

somefun-web  项目中的web模块(springboot项目)

somefun-service-system  system服务模块父工程

somefun-system-api  system服务模块api部分 存放server接口和dto 非常简单的一层

somefun-system-service   system服务具体逻辑实现模块

 

pom文件

这种开发的方式本质上还是将多个模块以jar包的方式引用进来,所以引用关系非常简单

somefun-web 引用 somefun-system-api和 somefun-system-service即可。

创建web项目时相当于独立模块创建,所以讲web模块中的pom文件部分内容移动到父项目中,具体修改后的内容如下

 

somefun 的pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6  
 7     <groupId>com.zhj</groupId>
 8     <artifactId>somefun</artifactId>
 9  
10     <parent>
11         <groupId>org.springframework.boot</groupId>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <version>2.1.1.RELEASE</version>
14         <relativePath/>
15     </parent>
16  
17     <packaging>pom</packaging>
18     <version>1.0-SNAPSHOT</version>
19     <modules>
20         <module>somefun-service-system</module>
21         <module>somefun-web</module>
22     </modules>
23  
24  
25     <dependencies>
26         <dependency>
27             <groupId>org.springframework.boot</groupId>
28             <artifactId>spring-boot-starter-web</artifactId>
29         </dependency>
30  
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-starter-test</artifactId>
34             <scope>test</scope>
35         </dependency>
36     </dependencies>
37  
38     <build>
39         <plugins>
40             <plugin>
41                 <groupId>org.apache.maven.plugins</groupId>
42                 <artifactId>maven-compiler-plugin</artifactId>
43                 <version>3.1</version>
44                 <configuration>
45                     <source>${java.version}</source>
46                     <target>${java.version}</target>
47                 </configuration>
48             </plugin>
49  
50             <plugin>
51                 <groupId>org.apache.maven.plugins</groupId>
52                 <artifactId>maven-surefire-plugin</artifactId>
53                 <version>2.19.1</version>
54                 <configuration>
55                     <skipTests>true</skipTests>
56                 </configuration>
57             </plugin>
58         </plugins>
59     </build>
60  
61 </project>

 

somefun-web 的pom.xml文件

我们需要在pom中指定下启动类文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4  
 5     <parent>
 6         <artifactId>somefun</artifactId>
 7         <groupId>com.zhj</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10  
11     <modelVersion>4.0.0</modelVersion>
12  
13     <groupId>com.zhj</groupId>
14     <artifactId>somefun-web</artifactId>
15     <version>0.0.1-SNAPSHOT</version>
16     <packaging>jar</packaging>
17     <name>somefun-web</name>
18  
19     <dependencies>
20  
21         <dependency>
22             <groupId>com.zhj</groupId>
23             <artifactId>somefun-system-api</artifactId>
24             <version>1.0-SNAPSHOT</version>
25         </dependency>
26  
27         <dependency>
28             <groupId>com.zhj</groupId>
29             <artifactId>somefun-system-service</artifactId>
30             <version>1.0-SNAPSHOT</version>
31         </dependency>
32     </dependencies>
33  
34  
35     <build>
36         <plugins>
37             <plugin>
38                 <groupId>org.springframework.boot</groupId>
39                 <artifactId>spring-boot-maven-plugin</artifactId>
40                 <configuration>
41                     <mainClass>com.zhj.somefun.web.SomefunWebApplication</mainClass>
42                     <layout>ZIP</layout>
43                 </configuration>
44                 <executions>
45                     <execution>
46                         <goals>
47                             <goal>repackage</goal>
48                         </goals>
49                     </execution>
50                 </executions>
51             </plugin>
52         </plugins>
53     </build>
54  
55 </project>

 

somefun-system-api 的pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>somefun-service-system</artifactId>
 7         <groupId>com.zhj</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     <version>1.0-SNAPSHOT</version>
12     <artifactId>somefun-system-api</artifactId>
13 </project>

 

somefun-system-service 的pom.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>somefun-service-system</artifactId>
 7         <groupId>com.zhj</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11  
12     <artifactId>somefun-system-service</artifactId>
13     <dependencies>
14  
15         <dependency>
16             <groupId>com.zhj</groupId>
17             <artifactId>somefun-system-api</artifactId>
18             <version>1.0-SNAPSHOT</version>
19         </dependency>
20     </dependencies>
21  
22 </project>

 

我们添加下测试代码

 

somefun-system-api 模块

 1 package com.zhj.somefun.system.api.dto;
 2  
 3 public class TestDto{
 4     private Integer id;
 5     private String name;
 6     public Integer getId() {
 7         return id;
 8     }
 9     public void setId(Integer id) {
10         this.id = id;
11     }
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public Integer getAge() {
19         return age;
20     }
21     public void setAge(Integer age) {
22         this.age = age;
23     }
24     private Integer age;
1 package com.zhj.somefun.system.api.service;
2  
3 import com.zhj.somefun.system.api.dto.TestDto;
4  
5 import java.util.List;
6  
7 public interface TestService {
8     public List<TestDto> getTestList();
9 }

somefun-system-service 模块

 1 package com.zhj.somefun.system.service.impl;
 2  
 3 import com.zhj.somefun.system.api.dto.TestDto;
 4 import com.zhj.somefun.system.api.service.TestService;
 5 import org.apache.el.stream.Stream;
 6 import org.springframework.stereotype.Service;
 7  
 8 import java.util.ArrayList;
 9 import java.util.List;
10  
11 @Service
12 public class TestServiceImpl implements TestService {
13  
14     @Override
15     public List<TestDto> getTestList() {
16         List<TestDto> list = new ArrayList<>();
17         TestDto dto = new TestDto();
18         dto.setAge(18);
19         dto.setId(1);
20         dto.setName("姓名");
21         list.add(dto);
22         return list;
23     }
24 }

 

somefun-web模块

 1 package com.zhj.somefun.web.controller;
 2  
 3 import com.zhj.somefun.system.api.dto.TestDto;
 4 import com.zhj.somefun.system.api.service.TestService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.GetMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8  
 9 import java.util.List;
10  
11 @RestController
12 public class Testcontroller {
13  
14     @Autowired
15     TestService testService;
16  
17     @GetMapping("/sayhello")
18     public String sayHello() {
19         return "Hello Word";
20     }
21  
22     @GetMapping("/getlist")
23     public List<TestDto> getlist(){
24         return testService.getTestList();
25     }
26 }
 1 package com.zhj.somefun.web;
 2  
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.annotation.ComponentScan;
 6  
 7 @SpringBootApplication (scanBasePackages={"com.zhj.somefun"})
 8 public class SomefunWebApplication {
 9  
10     public static void main(String[] args) {
11         SpringApplication.run(SomefunWebApplication.class, args);
12     }
13 }

 

 

SpringBoot在启动过程中会自动扫描启动类所在包名下的注解,如果不在启动类的包名下边的话就不会扫描到

咱们的启动类SomefunWebApplication所在的报名是com.zhj.somefun.web,system-service中的服务所在的报名为com.zhj.somefun.system.service 类并不在com.zhj.somefun.web下,所以启动类在扫描注解的时候不会扫描到service类,所以需要修改启动类代码。

1.将SomefunWebApplication类移动到com.zhj.somefun包下,这样扫描的时候可以将服务一起扫描到。

2.在启动类加上注解指定扫描包 scanBasePackages={"com.zhj.somefun"}

我们采用注解的方式

 

启动程序 访问域名 http://localhost:8080/getlist

会看到返回值:

[{"id":1,"name":"姓名","age":18}]

 

多模块程序打包

我们使用idea工具进行打包

选择父项目somefun

双击package

 

 

打包成功:

 

 

找到target目录中会发现已经打包好的jar文件

 

 

cmd切换到此目录,执行命令

java -jar somefun-web-0.0.1-SNAPSHOT.jar

 

我们再次访问域名 http://localhost:8080/getlist

会看到返回值:

[{"id":1,"name":"姓名","age":18}]

 

内容来源于网络如有侵权请私信删除