一般来说创建一个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文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.zhj</groupId>
<artifactId>somefun</artifactId> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent> <packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>somefun-service-system</module>
<module>somefun-web</module>
</modules> <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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build> </project>
somefun-web 的pom.xml文件
我们需要在pom中指定下启动类文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent>
<artifactId>somefun</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion> <groupId>com.zhj</groupId>
<artifactId>somefun-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>somefun-web</name> <dependencies> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.zhj.somefun.web.SomefunWebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
somefun-system-api 的pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>somefun-service-system</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>
<artifactId>somefun-system-api</artifactId>
</project>
somefun-system-service 的pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>somefun-service-system</artifactId>
<groupId>com.zhj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>somefun-system-service</artifactId>
<dependencies> <dependency>
<groupId>com.zhj</groupId>
<artifactId>somefun-system-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </project>
我们添加下测试代码
somefun-system-api 模块
package com.zhj.somefun.system.api.dto; public class TestDto{
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private Integer age;
package com.zhj.somefun.system.api.service; import com.zhj.somefun.system.api.dto.TestDto; import java.util.List; public interface TestService {
public List<TestDto> getTestList();
}
somefun-system-service 模块
package com.zhj.somefun.system.service.impl; import com.zhj.somefun.system.api.dto.TestDto;
import com.zhj.somefun.system.api.service.TestService;
import org.apache.el.stream.Stream;
import org.springframework.stereotype.Service; import java.util.ArrayList;
import java.util.List; @Service
public class TestServiceImpl implements TestService { @Override
public List<TestDto> getTestList() {
List<TestDto> list = new ArrayList<>();
TestDto dto = new TestDto();
dto.setAge(18);
dto.setId(1);
dto.setName("姓名");
list.add(dto);
return list;
}
}
somefun-web模块
package com.zhj.somefun.web.controller; import com.zhj.somefun.system.api.dto.TestDto;
import com.zhj.somefun.system.api.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class Testcontroller { @Autowired
TestService testService; @GetMapping("/sayhello")
public String sayHello() {
return "Hello Word";
} @GetMapping("/getlist")
public List<TestDto> getlist(){
return testService.getTestList();
}
}
package com.zhj.somefun.web; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication (scanBasePackages={"com.zhj.somefun"})
public class SomefunWebApplication { public static void main(String[] args) {
SpringApplication.run(SomefunWebApplication.class, args);
}
}
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}]