本准备写点代码实例放到网站,太多的模板,反而每次新建工程的时候很麻烦。于是准备把这个章节的内容提前先讲讲。正好把这个代码也管理起来。话说这个多模块功能还挺爽。
写过 C# 项目用过 Visual Studio 的人已经用惯了 一大把的项目放在一个解决方案中,下面我来实践一下 Java Spring Boot 的玩法。
目录
本章演示的多模块之间的关系如下图:spring-boot-study-helloworld 依赖人 spring-boot-study-helloworld-service 调用其方法 message 输出字符串。
本章节主要实现
使用 IDEA 创建一个 Maven 工程
1) File>New>Project,如下图选择 maven ,注意 Create from archetype 不能打钩,然后点击 【Next】下一步。
2) 填写GroupId(包名)、Artifact(项目名) 即可。点击 【Next】下一步
<?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.fishpro</groupId>
<artifactId>springstudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
5) 删除 src 文件
6) 注意如果您是创建的 spring 项目作为根项目,那么您需要删除 src、mvnw、mvnw.cmd、HELP.md、.mvn 文件
子项目可以是类库项目、Web应用项目
spring-boot-study-helloworld
,至此子项目已经添加完了,但你会发现,子项目不能允许调试,必须在根目录下的 pom.xml 配置此项目才行。spring-boot-study-helloworld
<?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.fishpro</groupId>
<artifactId>springstudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>spring-boot-study-helloworld</module>
</modules>
</project>
application.properties
为 application.yml
增加端口设置,为了和前天模块不冲突server:
port: 8082
package com.fishpro.helloworld.service;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String message(){
return "this is module for helloworld.service method message";
}
}
有的时候一个项目的 dao、service、controller 是分在不同的模块中,那么他们之间就有一些依赖关系,通常这些依赖关系是单向的。
本项目中项目 spring-boot-study-hellowrld
依赖项目 spring-boot-study-helloworld-service
,那么在 spring-boot-study-hellowrld
模块下 pom.xml 中设置
<dependency>
<groupId>com.fishpro.helloworld</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
我们在 helloworld 项目下新建 controller.IndexController
package com.fishpro.helloworld.controller;
import com.fishpro.helloworld.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Autowired
MyService myService;
@GetMapping("/say")
public String say(){
return myService.message();
}
}
在浏览器中 输入 http://localhost:8082/say 显示 hellowrld 项目调用成功
this is module for helloworld.service method message
我们再建立一个项目,项目名称 spring-boot-study-log 方法同第 2 节一样。最终效果图如下:
至此,我们发现可以多个 web application 是可以共存的。所以我们后面的所有示例都是在这个项目中体现。
IDEA 中也可以通过图形化管理模块
在 File > Project Structrue 中管理项目的模块
本项目中 application.properties
已经重新命名为 application.yml
IDEA 创建 Spring Boot 多模块项目(Multi Modules)
原文:https://www.cnblogs.com/fishpro/p/11165827.html