一般來說創建一個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}]