栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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



    com.luntek
    certificate_service
    0.0.1-SNAPSHOT


实战思路二(成功):

换个思路:将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项目重新编译一下,然后启动项目

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

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1040096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号