SpringBoot模块化开发之模块循环依赖问题

项目场景:

项目结构:启动类模块:certificate_service(pom.xml中添加了通用模块和考试模块作为依赖)、通用模块:certificate_service_commons、考试模块:certificate_service_exam(pom.xml中导入了通用模块作为依赖)。

业务需求:考试模块需要调用启动类主模块的userMapper的函数获取数据库中的用户数据。

 


实战思路一(失败):

起初想着在考试模块的pom.xml中添加主模块作为依赖不就可以了吗,试过之后会发现在编译的时候,会产生循环依赖的错误Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [certificate_service,certificate_service_exam] are excluded from annotation processing

<!--在考试模块中添加启动类模块-->
<dependency>
    <groupId>com.luntek</groupId>
    <artifactId>certificate_service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>


实战思路二(成功):

换个思路:将UserMapper文件放到通用模块commons中,这样启动类主模块可以用到,考试模块也可以用到,达到解决问题的目的。

步骤一:将UserMapper文件从主模块移动到通用模块中去

步骤二:启动类中的@MapperScan注解中添加通用模块Mapper文件路径注入到容器以便扫描加载使用。

package com.luntek.certificate;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableScheduling
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.luntek"})
@MapperScan(basePackages = {"com.luntek.certificate.mapper","com.luntek.commons.mapper",
        "com.luntek.exam.mapper"})
@SpringBootApplication
public class CertificateServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(CertificateServiceApplication.class, args);
    }

}

 步骤三:考试模块使用userMapper时,依赖注入通用模块下的UserMapper即可使用了

import com.luntek.commons.mapper.UserMapper;

public class UseUserMapperServiceImpl implements UserUserMapperService{

    @Autowired
    private UserMapper userMapper;

    public String useUserMapperMethod(String Param){
        String userId = userMapper.getUserIdByParam(Param);
        return userId;
    }

}

步骤四:Rebuild project项目重新编译一下,然后启动项目

 结束:小小问题,记录一下,输出巩固一下

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot模块化开发是指使用子模块对项目进行组织和管理。在Spring Boot中,可以通过在主工程的pom.xml文件中添加子模块信息来实现模块化开发。这样可以使主工程能够访问和使用子模块中的类、资源和配置。 在开发中,可以使用spring-boot-maven-plugin插件来配置不让当前模块作为启动模块进行编译。可以通过在插件配置中设置skip为true来实现该功能,这样编译时就会跳过当前模块。 如果需要使用子模块进行组织,可以按照以下步骤进行操作:<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [知识点19--springboot模块开发](https://blog.csdn.net/dudadudadd/article/details/127142477)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [模块化开发:掌握Spring Boot模块的使用技巧](https://blog.csdn.net/fgliu/article/details/131269308)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热心码民阿振

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值