700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > source tree 递归子模块_多模块 Spring Boot 项目

source tree 递归子模块_多模块 Spring Boot 项目

时间:2022-01-19 13:28:09

相关推荐

source tree 递归子模块_多模块 Spring Boot 项目

在开发一个稍具规模的项目时,我们常常希望能够将程序的各个部分分成不同的模块,加以管理。本完简单介绍了如何建立一个多模块的 Spring Boot 项目。

假设我们需要开发一个包含 2 个子模块的项目:app子模块是上层应用,service子模块提供服务。

代码:/chenf42/spring-boot-examples/tree/master/multi-module-project

1. 模块配置

使用Spring Initializr新建一个 Spring Boot 项目。记得选中Spring Web依赖。

修改pom.xml文件,将打包模式改为pom方式,即添加或修改<packaging>tag 为:

<packaging>pom</packaging>

并添加<modules>tag 来指示我们的子模块:

<modules><module>app</module><module>service</module></modules>

这个项目主模块是不直接运行的,我们可以删掉<build>tag 和src文件夹。

在项目管理视图上右击multi-module-project项目名称,添加app模块:

使用同样的方式添加另一个名为service的子模块。

我们得要把项目模块设置为各子模块的父模块,将app/pom.xmlservice/pom.xml中的<parent>tag 修改为如下内容:

<parent><groupId>com.meta-object</groupId><artifactId>multi-module-project</artifactId><version>0.0.1-SNAPSHOT</version></parent>

因为service子模块只用来给app提供服务,而不会被直接运行,我们可以把service/pom.xml中的<build>tag 删掉,并删掉不需要的service/src/main/java/com/metaobject/multimoduleproject/service/ServiceApplication.java文件。

同时,我们需要修改app/pom.xml,添加对service模块的依赖:

<dependency><groupId>com.meta-object.multi-module-project</groupId><artifactId>service</artifactId><version>0.0.1-SNAPSHOT</version></dependency>

2. 功能实现

添加service/src/main/java/com/metaobject/multimoduleproject/service/User.java文件:

package com.metaobject.multimoduleproject.service;public class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}

添加service/src/main/java/com/metaobject/multimoduleproject/service/UserService.java文件:

package com.metaobject.multimoduleproject.service;import org.springframework.stereotype.Service;@Servicepublic class UserService {public User getDefaultAdmin() {User user = new User();user.setName("admin");return user;}}

这里我们提供了一个最简单的“服务”:查询默认管理员信息。

添加app/src/main/java/com/metaobject/multimoduleproject/app/UserController.java文件:

package com.metaobject.multimoduleproject.app;import com.metaobject.multimoduleproject.service.User;import com.metaobject.multimoduleproject.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController {private UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}@GetMapping("/admin")public User showDefaultAdmin() {return userService.getDefaultAdmin();}}

注意这里 Intellij IDEA 会给出警告:Could not autowire. No beans of 'UserService' type found.

若我们运行app模块,会得到如下输出:

***************************APPLICATION FAILED TO START***************************Description:Parameter 0 of constructor in com.metaobject.multimoduleproject.app.UserController required a bean of type 'com.metaobject.multimoduleproject.service.UserService' that could not be found.Action:Consider defining a bean of type 'com.metaobject.multimoduleproject.service.UserService' in your configuration.

这是因为app模块没扫描到UserService这个服务。解决方法是在app/src/main/java/com/metaobject/multimoduleproject/app/AppApplication.java中导入此服务:

package com.metaobject.multimoduleproject.app;import com.metaobject.multimoduleproject.service.UserService;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Import;@SpringBootApplication@Import(UserService.class) // 导入 UserServicepublic class AppApplication {public static void main(String[] args) {SpringApplication.run(AppApplication.class, args);}}

3. 试一试!

此时重新运行app模块并访问http://localhost:8080/admin可以看到如下结果:

(完)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。