项目场景:
项目结构:启动类模块: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项目重新编译一下,然后启动项目
结束:小小问题,记录一下,输出巩固一下