springboot模块里面调用另外一个模块的方法实现
作者:dlage
在Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,本文就来介绍一下springboot模块里面调用另外一个模块的方法实现,感兴趣的可以了解一下
bug描述:
在一个springboot模块里面调用另外一个模块的方法
通过下面的代码可以看到,我的方法所在的模块是在com.bpmn.camunda.sync.provider里面,而我导入的包是在另外一个模块里面 com.bpmn.camunda.common.service 如果直接启动项目会报错。
package com.bpmn.camunda.sync.provider.server.impl;
import com.bpmn.camunda.common.service.IActHiCommentService;
@Service
public class CActIdUserServiceImpl extends AbstractSupperService<CActIdUserMapper, CActIdUserModel, CActIdUserDTO> implements CActIdUserService {
@Autowired
private IActHiCommentService iActHiCommentService;
项目背景:
普通的springclou项目。
解决方法:
我们应该在主方法里面加上包的扫描,springbooot会自动扫描到bean,并注入到Ioc容器里面。
一个是 加上componentScan 加上了 "com.bpmn.camunda.common.service"
@ComponentScan(basePackages = {"com.bpmn.camunda.auth","com.bpmn.camunda.sync","com.bpmn.camunda.common.service"})
@SpringBootApplication
@ComponentScan(basePackages = {"com.bpmn.camunda.auth","com.bpmn.camunda.sync","com.bpmn.camunda.common.service"})
@MapperScan({"com.bpmn.camunda.sync.provider.mapper","com.bpmn.camunda.common.mapper"})
@EnableDiscoveryClient
@EnableProcessApplication
@EnableFeignClients(basePackages = {"com.bpmn.camunda","com.focusin.bpmn"})
@EnableAopLog
public class BpmnCamundaSyncApplication {
public static void main(String[] args) {
SpringApplication.run(BpmnCamundaSyncApplication.class, args);
}
}
其实只加上了service方法还不够,因为service大概率会调用mapper方法,所以还需要加上mapper扫描。
@MapperScan({"com.bpmn.camunda.sync.provider.mapper","com.bpmn.camunda.common.mapper"})
总结:
添加其他模块的bean时,spring并不能直接扫描到该bean,需要我们手动设置扫描路径。
到此这篇关于springboot模块里面调用另外一个模块的方法实现的文章就介绍到这了,更多相关springboot模块调用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
